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...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
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 java class with name Dog. Instructions for Dog class:              This class is modeled after...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after a Dog. You should have instance variables as follows: The Dog’s name The number of tricks known by the Dog. The color*of the Dog’s coat (fur). *: For the Dog’s fur color you should use the Color class from the java.awt package. You should choose proper types and meaningful identifiers for each of these instance variables. Your class should contain two constructor methods. A...
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...