Question

java coding Create two objects S1 and S2 that uses the default constructor · Call the...

java coding
Create two objects S1 and S2 that uses the default constructor
· Call the ‘display’ method for objects S1 and S2.
· Call the ‘setGrade’method that increments the grade by 1 for object S2
· Call the ‘getGrade’ method that will display the value variable grade for objects S1 and S2

Homework Answers

Answer #1

class student
{
int grade;
String name;
student() //Default constructor
{
grade = 4;
name = "pqr";
}
void setGrade() //setGrade method
{
grade = grade + 1;
}
void display() //display method
{
System.out.println("Name: "+name);
System.out.println("Grade: "+grade);
}
int getGrade() //getGrade method
{
return grade;
}
}
public class Main
{
   public static void main(String[] args) {
       student s1 = new student(); //Creating first object
       student s2 = new student(); //Creating second object
       s1.display(); //Calling display method on object 1
       s2.display(); //Calling display method on object 2
       s2.setGrade(); //Calling setGrade method on object 2   
       System.out.println("first object final grade: "+s1.getGrade()); //Printing the grade for object 1
       System.out.println("second object final grade: "+s2.getGrade()); //Printing the grade for object 2
   }
}

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
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 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
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability...
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability to apply polymorphism in a Java program. Program Specifications: Design the following program: A person has a name, and age. An Employee is a person. An Employee has a company and position. A SoftwareEngineer is an employee and a person. A SoftwareEngineer has a rank (Junior, Middle, and Senior) A SoftwareEngineer either C programmer, Jave programmer or both. A Manager is an Employee and...
1. Suppose a system composed of two subsystems that we call S1 and S2 in thermal...
1. Suppose a system composed of two subsystems that we call S1 and S2 in thermal contact. Assume further that the wall separating the two subsystems is movable and impenetrable at particles. (a) Define the macrostate of the system. (b) Through statistical considerations find the equilibrium condition for such a system. (c) Determine the physical significance of the thermodynamic equilibrium parameters. (d) If the exchange is such that the three macroscopic parameters (E, V, N) are variable, find the new...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Write the following in Java In this section, you will overload methods to display dates. The...
Write the following in Java In this section, you will overload methods to display dates. The date-displaying methods might be used by many different applications in an organization, such as those that schedule jobs, appointments, and employee reviews. The methods take one, two, or three integer arguments. If there is one argument, it is the month, and the date becomes the first day of the given month in the year 2020. If there are two arguments, they are the month...
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...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
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)...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT