Question

Consider the Top class i public abstract class Topí 2 private 3 protected String name; 4 public intx - 12; int age; e public Top(String name)[ this.namename age0; System.out.println(x); 10 12 public abstract void foo(String f); 13 Create a Middle class that extends the Top class. The class should have a single constructor that takes two input parameters: a String (for name) and int (for age). Your constructor should not have any redundant code (i.e., code that is repeated elsewhere). The foo method of Middle should print (to standard output) the objects name and age. The foo method should not be able to be modified by a child class of Middle. Each Middle object also has an Integer attribute called middle that should be assigned to 42 when an object is created The Middle class should have an abstract method called bar that take no input parameters and returns voidAll question base on Java Se 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

B.

// Top.java

public abstract class Top {

      

       private int x=12;

       protected String name;

       public int age;

      

       public Top(String name)

       {

             this.name = name;

             age=0;

             System.out.println(x);

       }

      

       public abstract void foo(String f);

}

//end of Top.java

// Middle.java

public abstract class Middle extends Top{

      

       private int middle ;

      

       //constructor

       public Middle(String name, int age)

       {

             super(name); // calling Top constructor

             middle=42;

             this.age=age;

       }

      

       public final void foo(String f) // the method is final hence it cannot be modified by a child class of Middle

       {

             System.out.println("Name : "+name+" Age : "+age);

       }

       public abstract void bar(); // abstract method

}

//end of Middle.java

C.

//Box.java

public class Box implements Comparable<Box>{

      

       public String a;

       protected String b;

      

       public Box(String a, String b)

       {

             this.a = a;

             this.b = b;

       }

       @Override

       public final int compareTo(Box b2) {

             if(a.compareTo(b2.a) < 0)

                    return -1;

             else if(a.equals(b2.a) && ((a.length()+b.length()) < (b2.a.length()+b2.b.length())))

                    return -1;

             else if(a.equals(b2.a) && ((a.length()+b.length()) == (b2.a.length()+b2.b.length())))

                    return 0;

             else

                    return 1;

       }

}

//end of Box.java

D.

// Program.java

import java.util.Arrays;

public class Program {

       public static String letters[]= {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

       public static String LETTERS[]= {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

      

       public static Box[] vowelBox(Box boxes[])

       {

             Box vowels[] = new Box[0];

             for(int i=0;i<boxes.length;i++)

             {

                    if(boxes[i].compareTo(new Box("a","A")) == 0 || boxes[i].compareTo(new Box("e","E"))==0 || boxes[i].compareTo(new Box("i","I")) == 0

                                 || boxes[i].compareTo(new Box("o","O")) == 0 || boxes[i].compareTo(new Box("u","U")) == 0)

                    {

                          

                           vowels = Arrays.copyOf(vowels, vowels.length+1);

                           vowels[vowels.length-1] = boxes[i];

                    }

             }

             return vowels;

       }

      

       public static void main(String[] args) {

             if(args.length < 2)

             {

                    System.out.println("Insufficient arguments");

                    System.exit(0);

                   

             }

             int start = Integer.parseInt(args[0]);

             int stop = Integer.parseInt(args[1]);

            

             Box boxes[] = new Box[stop-start+1];

             System.out.println(" Boxes created : ");

             for(int i=start;i<=stop;i++)

             {

                    boxes[i-start]= new Box(letters[i-1],LETTERS[i-1]);

                    System.out.println(" a = "+boxes[i-start].a+" b = "+boxes[i-start].b);

             }

             System.out.println("Vowel Boxes : ");

             Box vowels[] = vowelBox(boxes);

            

             for(int i=0;i<vowels.length;i++)

                    System.out.println(" a = "+vowels[i].a+" b = "+vowels[i].b);

       }

}

//end of Program.java

Output:

Boxes created: a=c a- a=1b=1 a- VowelBoxes: a e a=1b=1

Add a comment
Know the answer?
Add Answer to:
All question base on Java Se 8 Consider the Top class i public abstract class Topí...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT