Question

Design a class with the following requirements: 1- give the class a name from your choice....

Design a class with the following requirements:

1- give the class a name from your choice.
2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables)
3- Initialize the variables using two different constructors.
4- Use mutator and accessor methods (set and get) for the private data members.
5- Display the value of each member variable using a method.
6- In the main, create an object to fill the class data and display the class information.


(in Java language)

Homework Answers

Answer #1

Here is your code, save it in Student.java file

import java.io.*;
public class Student
{
    public String name;
    private int roll;
    private int marks;

    Student(String name)
    {
        this.name = name;
    }
    Student(String firstname, String lastname)
    {
        this.name = firstname + ' ' + lastname;
    }
    public void setRoll(int roll)
    {
        this.roll = roll;
    }
    public int getRoll()
    {
        return this.roll;
    }
    public void setMarks(int marks)
    {
        this.marks = marks;
    }
    public int getMarks()
    {
        return this.marks;
    }
    public void printStudentDetails()
    {
        System.out.println("name: " + this.name);
        System.out.println("roll: " + this.getRoll());
        System.out.println("marks: " + this.getMarks());
    }
    public static void main(String[] args)
    {
        Student s1 = new Student("Harsh");
        s1.setRoll(12);
        s1.setMarks(94);
        System.out.println("Details of student 1: ");
        s1.printStudentDetails(); 
        
        Student s2 = new Student("Aryan", "Roy");
        s2.setRoll(21);
        s2.setMarks(49);
        System.out.println("\nDetails of student 2: ");
        s2.printStudentDetails(); 
    }
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Choose any entity of your choice to represent a superclass. It must not be any of...
Choose any entity of your choice to represent a superclass. It must not be any of the following: - Human, Student, Vehicle, Person, Car, Animal, Book • Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
(Use JAVA) For this program, you need to create three classes, one of which is a...
(Use JAVA) For this program, you need to create three classes, one of which is a main class that includes aggregation from the other two classes. Each of the classes must include at least two fields, multiple constructors, accessor and mutator methods, and toString. Methods in your main class must include: 1. .equals 2. .copy 3. One that passes an object other than the .copy method 4. One that returns an object 5. toString that calls the toStrings from the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT