Assume a class file exists as described by the UML diagram. School - roll : int
ID: 3598081 • Letter: A
Question
Assume a class file exists as described by the UML diagram.
School
- roll : int
- name : String
+ School (rollIn : int, nameIn : String)
+ School ()
+ setRoll ( rollIn : int) : void
+ setName ( nameIn : String) : void
+ getRoll () : int
+ getName ()
Write a main method which
· Makes an instance of this class using the default constructor.
· Calls the mutator methods to set the value of the data fields to 132 and “good St”.
· Makes a second instance of this class, setting the value of the data fields to 350 and “Hello St” via a constructor.
· Determines and displays the name of the school with the largest roll.
Explanation / Answer
There you go !
class School{
int roll;
String name;
School(){
// Does nothing
}
School(int rollIn,String nameIn){
roll = rollIn;
name = nameIn;
}
public void setRoll(int rollIn){
roll = rollIn;
}
public void setName(String nameIn){
name = nameIn;
}
public int getRoll(){
return roll;
}
public String getName(){
return name;
}
}
class DriverClass{
public static void main(String[] args) {
School inst1 = new School();
inst1.setRoll(132);
inst1.setName("good St");
School inst2 = new School(350,"Hello St");
//Printing School with largest roll number using ternary operator
/*
A ternary operator works the following way
System.out.print( 3 > 2 ? "Foo" : "Bar" );
Output: Foo
System.out.print( 1 > 2 ? "Foo" : "Bar" );
Output: Bar
*/
// It will display Hello St for obvious reasons
System.out.println((inst1.getRoll() > inst2.getRoll() ? inst1.getName() : inst2.getName()));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.