In: Computer Science
Given the following definition of class Aclass:
class Aclass
{
private char letter;
private int value;
public Aclass( )
{
Letter = ‘A’;
value = 0;
}
public Aclass( char ch, int num)
{
letter = ch;
value = num;
}
public int getvalue( )
{
return value;
}
public void print( )
{
System.out.println( “letter =” + letter + “\n value =” + value);
}
}
A. ( 20 pts )
B. ( 27 pts )
Assume given the following definitions of objects and static method process( ):
Aclass aobj1 = new Aclass( ‘B’, 5) ;
Aclass aobj2 = new Cclass( ‘Z’, 20, 30 ),;
Cclass cobj = new Cclass( ‘K’, 90, 70 );
static void process( Aclass obj )
{
obj.print( );
}
aobj1.print( );
aobj2.print( );
process( aobj1 );
process( cobj );
A. Define and instantiate the array Alist of 10 objects of the class Aclass. Each object in the array is initialized with the default constructor. (6 pts)
Below are your classeS:
Bclass.java
class Bclass {
private int firstm;
private Aclass secondm;
Bclass() {
this.firstm = 10;
secondm = new Aclass();
}
public Bclass(int firstm, Aclass secondm) {
this.firstm = firstm;
this.secondm = secondm;
}
public void print() {
System.out.println("firstm=" + firstm);
System.out.println("secondm: ");
secondm.print();
}
public int computeSum() {
return this.firstm + secondm.getvalue();
}
public static void main(String[] args) {
Bclass bobj = new Bclass(30, new Aclass('Z', 20));
System.out.println("Value of bobj: ");
bobj.print();
System.out.println("Sum: " + bobj.computeSum());
}
}
Output
Cclass.java
public class Cclass extends Aclass {
private int firstm;
public Cclass() {
super();
this.firstm = 10;
}
public Cclass(char ch, int num, int firstm) {
super(ch, num);
this.firstm = firstm;
}
@Override
public void print() {
System.out.println("firstm: " + this.firstm);
super.print();
}
public int computeSum() {
return this.firstm + super.getvalue();
}
}
First question:
Assume given the following definitions of objects and static method process( ):
Aclass aobj1 = new Aclass( ‘B’, 5) ;
Aclass aobj2 = new Cclass( ‘Z’, 20, 30 ),;
Cclass cobj = new Cclass( ‘K’, 90, 70 );
static void process( Aclass obj )
{
obj.print( );
}
aobj1.print( );
aobj2.print( );
process( aobj1 );
process( cobj );
For this changed Cclass as follows:
public class Cclass extends Aclass {
private int firstm;
public Cclass() {
super();
this.firstm = 10;
}
public Cclass(char ch, int num, int firstm) {
super(ch, num);
this.firstm = firstm;
}
@Override
public void print() {
System.out.println("firstm: " + this.firstm);
super.print();
}
public int computeSum() {
return this.firstm + super.getvalue();
}
public static void main(String[] args) {
Aclass aobj1 = new Aclass('B', 5);
Aclass aobj2 = new Cclass('Z', 20, 30);
Cclass cobj = new Cclass('K', 90, 70);
aobj1.print( );
aobj2.print( );
process( aobj1 );
process( cobj );
}
static void process(Aclass obj)
{
obj.print();
}
}
Output
letter =B
value =5
firstm: 30
letter =Z
value =20
letter =B
value =5
firstm: 70
letter =K
value =90
Second Question:
is aobj2.computeSum( ); legal
No because aobj2 is object of Aclass and Aclass doesnt have computeSum function
Third Question
If it is not legal, how would you correct the problem?
To correct it we need to change below line
Aclass aobj2 = new Cclass('Z', 20, 30);
to
Cclass aobj2 = new Cclass('Z', 20, 30);
This will solve the issue
Fourth Question:
Define and instantiate the array Alist of 10 objects of the class Aclass. Each object in the array is initialized with the default constructor.
Aclass Alist[] = new Aclass[10];
Alist[0] = new Aclass();
Alist[1] = new Aclass();
Alist[2] = new Aclass();
Alist[3] = new Aclass();
Alist[4] = new Aclass();
Alist[5] = new Aclass();
Alist[6] = new Aclass();
Alist[7] = new Aclass();
Alist[8] = new Aclass();
Alist[9] = new Aclass();