Question

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.

  1. 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?

  2. 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 the instance variables to default values, one that has 4 parameters to set the instance variables, and one that has 1 parameter for the most important instance variable and uses defaults for the others.

  3. Add a print() method that uses System.out.println to print out all the instance variables.

  4. Add a main method that constructs at least 3 Student objects using the 3 different constructors and then calls their print() methods.

  5. /** class Student
    * with 4 instance variables,
    * 3 constructors, a print method,
    * and a main method to test them.
    */
    public class Student
    {
    // Write 4 instance variables


    // Write 3 constructors to initialize the instance variables
    // 1. no parameters using default values
    // 2. 1 parameter and the rest default values
    // 3. 4 parameters


    // Write a print method that prints all the instance variables
    public void print()
    {


    }

    // main method
    public static void main(String[] args)
    {
    // Construct 3 Student objects using the 3 different constructors


    // call their print() methods

    }
    }

Homework Answers

Answer #1

Basic details of a Student is Name, age, Rollno and branch. Consider the below example instance variables and their data types.

Name - It is a string value

Age - It is integer value

Roll-no - It is a string value

Branch - It is a String value

Constructor:

constructors are the methods used to initialise the instace variables/objects of a class. when an object is created for a class then automatically constructor is called.

Program:

public class Student
{
   String name;
   int age;
   String rollno;
   String branch;
  
   public Student() //Default Constructor
   {
       this.name="Hari";
       this.age=20;
       this.rollno="5R3";
       this.branch="IT";      
   }

   public Student(String name) //Parameterised constructor with 1 parameter
   {
       this.name=name;
       this.age=25;
       this.rollno="5R3";  
       this.branch="CSE";
      
   }

   public Student(String name,int age,String rollno,String branch) //Parameterised constructor with 4 parameters
   {
       this.name=name;
       this.age=age;
       this.rollno=rollno;
       this.branch=branch;
   }
// print method to display the instace variables
   public void print()
   {
       System.out.println("Student Details:");
       System.out.println("Name: "+this.name);
       System.out.println("Age: "+this.age);
       System.out.println("Roll-No: "+this.rollno);
       System.out.println("Branch: "+this.branch);
       System.out.println();
      
   }
   public static void main(String args[])
   {
       Student s1=new Student();
       Student s2=new Student("Sri");
       Student s3=new Student("Hareesh",20,"5d3","ECE");
       s1.print();
       s2.print();
       s3.print();
   }
  
}

Output:

E:\>javac Student.java

E:\>java Student
Student Details:
Name: Hari
Age: 20
Roll-No: 5R3
Branch: IT

Student Details:
Name: Sri
Age: 25
Roll-No: 5R3
Branch: CSE

Student Details:
Name: Hareesh
Age: 20
Roll-No: 5d3
Branch: ECE

Explanation:

In Object oriented programming, when an object is created for a class, complier searches for a constructor to execute, after that only it xecutes other methods. Here we created 3 different constuctors with 0 parameters, 1 parameter, 4 parameters respectively. If we do not pass arguments to the constructors, it take the default values.

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
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
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...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
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,...
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,...
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...
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
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...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT