Question

In: Computer Science

Given the following definition of class Aclass:             class Aclass { private char letter;                  

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 )

  1. Write the definition of the class named Bclass as follows:
  • It has the private instance variable firstm (integer) and secondm (reference variable of the class Aclass).
  • It has the default constructor (the initial value for instance variable firstm is 10) and the constructor with a parameter for each of its instance variables.
  • It has a public instance method void print( ) that outputs the values of its instance variables.
  • It has a public instance method int computeSum(   ) that computes and returns the sum of the instance variables secondm.value and firstm.
  1. Write a statement to define the object of the class Bclass named bobj initialized with the value ‘Z’ for instance variable secondm.letter, 20 for instance variable secondm.value, and 30 for instance variable firstm.

B.  ( 27 pts )

  1. Write the definition of the class named Cclass that extends class Aclass as follows:
  • It has an additional private instance variable firstm (integer value).
  • It has the default constructor (the initial value for instance variable firstm is 10) and the constructor with a parameter for each of its instance variables.
  • The instance method print( ) is overridden in this class: it also outputs the value of instance variable firstm in addition to the instance variables inherited.
  • It has a public instance method int computeSum(   ) that computes and returns the sum of the instance variables value and firstm.

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(   );

      }

  1. Show the output produced by each of the following statements:

                        aobj1.print(   );

                        aobj2.print(   );

                        process( aobj1 );

                        process( cobj );

  1. Is the statement             aobj2.computeSum(   ); legal? Explain. If it is not legal, how would you correct the problem?

                       

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)

Solutions

Expert Solution

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(   );

      }

  1. Show the output produced by each of the following statements:

                        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();


Related Solutions

A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
In Java What is the output produced by the following code? char letter = 'B'; switch...
In Java What is the output produced by the following code? char letter = 'B'; switch (letter) { case'A': case'a': System.out.println("Some kind of A."); case'B': case'b': System.out.println("Some kind of B."); break; default: System.out.println("Something else."); break; }
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
For the following terms find the correct definition below and place the letter of that response...
For the following terms find the correct definition below and place the letter of that response in the blank space next to the term. Each definition is used only once – there are four terms that are not used. business process BPM software BPO business-without-boundaries CRM software off-shoring RFID tags Sales process mnemonic codes group code CFE SAR CISA ERP XBRL ISACA IT REA KPI VAR a. A certification that requires individuals to meet certain qualification set by the Association...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT