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
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...
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,...
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.
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
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)...
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...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT