Question

This should be a simple code for beginning Java; Create a Student class with four instance...

This should be a simple code for beginning Java;

Create a Student class with four instance variables, a default constructor, a 4-arg constructor, and getters and setters for each instance variable.

Homework Answers

Answer #1

SOURCE CODE FOR STUDENT CLASS:

import java.util.*;
import java.lang.*;
import java.io.*;

class Student
{
String firstName; //declaring four instance variables..
String lastName; //instance variables are those which are inside a class
int rollNo;//but outside a method or constructor..
int marks;

public Student()//default constructor..
{
System.out.println("Default constructor called..");
}
public Student(String fName, String lName, int rNo, int m) //parameterized constructor with 4 variables..
{
firstName=fName;
lastName=lName;
rollNo=rNo;
marks=m;
}

public String getFirstName(){ //just returning the value of instance variable which you want..
return firstName;

}

public String getLastName(){ //just returning the value of instance variable which you want..
return lastName;
}

public int getRollNo(){ //just returning the value of instance variable which you want..
return rollNo;
}

public int getMarks(){ //just returning the value of instance variable which you want..
return marks;
}

public void setFirstName(String fName){
firstName=fName; //setting firstName i.e. instance variable to the name passed in set method..
}

public void setLastName(String lName){
lastName=lName; //setting lastName i.e. instance variable to the name passed in set method..
}

public void setRollNo(int rNo){
rollNo=rNo; //setting rollNo i.e. instance variable to the integer passed in set method..
}

public void setMarks(int m){
marks=m; //setting marks i.e. instance variable to the integer passed in set method..
}
}

LETS SEE AN EXAMPLE BY MAKING A MAIN CLASS WHICH EXECUTE ABOVE METHODS i.e. GETTER AND SETTER:

SOURCE CODE FOR EXAMPLE CLASS:

class Example
{
public static void main(String[] args){

Student tushar = new Student ("Tushar", "Nagpal", 80, 100); //creating an object using parameterized constructor..
System.out.println("Values of instance variables before using setters: ");
System.out.println(tushar.getFirstName() + " " + tushar.getLastName()+" " + tushar.getRollNo()+" " +tushar.getMarks());

tushar.setFirstName("MODIFIED"); //changing values using setters..
tushar.setLastName("CHANGED");
tushar.setRollNo(1);
tushar.setMarks(0);

System.out.println("Values of instance variables after using setters: ");
System.out.println(tushar.getFirstName() + " " + tushar.getLastName()+" " + tushar.getRollNo()+" "+tushar.getMarks());
//printing values using getters..
}
}

OUTPUT OF EXAMPLE:

NOTE: In the question it was written to create a Student class.. but I have taken an "Example" class also which is main class. So that you are able to see whats going on really..and it is optional..

So, Please copy entire code of both the classes separately and save them in a common directory with the name:

"Student.java" and "Example.java"

then, compile both and run "Example" class..

I have mentioned the comments wherever neccessary in the code..

HOPE IT HELPS..!!!!

For any query or problem please feel free to comment.

Thanks.

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...
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 the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
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 an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
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 the 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. PLEASE CODE THIS IN JAVA
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();...
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...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
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.
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT