Question 1: Assuming that the following classes have been defined: public class
ID: 3860201 • Letter: Q
Question
Question 1:
Assuming that the following classes have been defined:
public class A
{
public static void method1()
{
System.out.println("A1");
}
}
public class B extends A
{
public static void method2()
{
System.out.println("B2");
}
}
public class C extends B
{
public static void method1()
{
System.out.println("C1");
}
}
And assuming the following objects have been defined:
A a = new A();
B b = new B();
B other2 = new C();
In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement causes an error, fill in the right-hand column with either the phrase "compiler error" or "runtime error" to indicate when the error would be detected.
Statement Output
---------------------------------------------------------------------
a.method1(); __________________
a.method2(); __________________
b.method1(); __________________
b.method2(); __________________
other2.method1(); __________________
other2.method2(); __________________
Question 2:
Write Java code to create a new class called “Student”. An object of the Student class has private attributes - student id, name and address. Instantiate an array object of this class, with these attributes (for 2 objects). Thereafter, display these attributes to a user. (Your code should include entire class and a driver to demonstrate the above).
Explanation / Answer
a.method1(); A1
a.method2(); Compiler Error
b.method1(); A1
b.method2(); B2
other2.method1(); A1
other2.method2(); B2
__________________________
Question 2)
Student.java
public class Student {
// Declaring instance variables
private int id;
private String name;
private String address;
// Parameterized constructor
public Student(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
_______________________
TestStudent.java
public class TestStudent {
public static void main(String[] args) {
//Creating two Student class Obejcts
Student st1 = new Student(111, "Ken Jones", "Park Street,New York");
Student st2 = new Student(222, "Mahesh Roy", "Church Street,WashingTon DC");
//Calling the toString() methods on the Student class Objects
System.out.println(st1.toString());
System.out.println(st2.toString());
}
}
______________________
Output:
Student [id=111, name=Ken Jones, address=Park Street,New York]
Student [id=222, name=Mahesh Roy, address=Church Street,WashingTon DC]
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.