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

Hi,

Hope you are doing fine. I have cooded the program in java as per your requirement. The code is clearly explained using comments that have been highlighted in bold. Also, in the main methos I have initialized two objects in order to depict the difference between the parameterized and non parameterized constructors that are used during initialization of an object. You may change the main method as per your requirement.

Program:

//class to store information about a student
public class StudentData {
   //declaring instance variables
   //name is a public string variable that stores name of student

   public String name;
   // marks is a public double variable that stores the marks of a student
   private double marks;
   //department is a private string variable that stores the department of a student
   private String department;
  
   //initializing the variable using a non parameterized constructor
   public StudentData() {
       this.name="none";
       this.marks=0;
       this.department="none";
   }

   //initializing the variable using a parameterized constructor
   public StudentData(String name, double marks, String department) {
       this.name = name;
       this.marks = marks;
       this.department = department;
   }
  
   //getters and setters for private data members
   //getter for marks

   public double getMarks() {
       return marks;
   }
   //setter for marks
   public void setMarks(double marks) {
       this.marks = marks;
   }
   //getter for department
   public String getDepartment() {
       return department;
   }
   //setter for department
   public void setDepartment(String department) {
       this.department = department;
   }
   //method to display the value of each member variable using a method.
   public void display()
   {
       System.out.println("Name: "+name);
       System.out.println("Marks: "+marks);
       System.out.println("Department: "+department);
   }

   public static void main(String[] args) {
       //creating new object s1 using non parameterized constructor of StudentData
       StudentData s1=new StudentData();
       //creating new object s1 using non parameterized constructor of StudentData
       StudentData s2=new StudentData("Harry",90,"Computer Science");
       //printing contents of s1
       System.out.println("S1 before setting values manually: ");
       s1.display();
      //setting values to instance variable of s1 objects.
       //since name is public we need not use a setter to set some value

       s1.name="Ron";
       //using setters for marks and department
       s1.setMarks(40);
       s1.setDepartment("Electronics");
       //printing contents of s1
       System.out.println("\nS1 after setting values manually: ");
       s1.display();
       //printing contents of s1
       System.out.println("\nValues of S2: ");
       s2.display();

   }

}

Executable code snippet:

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.
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT