Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the class whose diagram is shown below and write a driver program (progres

ID: 3881504 • Letter: W

Question

Write the class whose diagram is shown below and write a driver program (progressively developed) to test the class (verify that all the methods function properly). The default (no-parameter constructor) should initialize the String data member name to " " and numeric data member to zero. The input method should allow the user to input the values of an object's data members.

Then code the definition of the class whose diagram follows. The constructors should allocate the array(data) of Listing object references sized to either 100 elements (the no-parameter constructor) or the number of elements specified by the argument sent to the one-parameter constructor. The method, addListing, will add a new Listing object to the array at index next. The showAll method will output the values of the data members of all the Listing objects. Write a progressively developed driver program to demonstrate that the class functions properly.

Listing String name int age + Listing() + Listing (String, int) + string toString(O) + void input () + void setName (String) + void setAge (String) + String getName (O + int getAge ( )

Explanation / Answer


//Listing.java
import java.util.Scanner;
public class Listing {  
   private String name;
   private int age;
  
   //constructor
   public Listing() {
       name=" ";
       age=0;
   }
  
   //parameterconstructor
   public Listing(String name,
           int age) {
       this.name=name;
       this.age=age;
   }
   //Override toString method
   public String toString() {  
       return String.format("%-20s%-10d",name,age);
   }  
   //input method to prompt for name and age
   public void input(){
       Scanner scanner=new Scanner(System.in);
       System.out.println("Enter name :");
       name=scanner.nextLine();
       System.out.println("Enter age :");
       age=Integer.parseInt(scanner.nextLine());
   }  
   //Method to set name
   public void setName(String name){
       this.name=name;
   }
   //Method to set age
   public void setAge(int age){
       this.age=age;
   }
   //Method to get name
   public String getName(){
       return name;
   }
   //Method to get age
   public int getAge(){
       return age;
   }
}//end of the Listing class

----------------------------------------------------------------------------

//ListingArray.java
public class ListingArray {
  
   private Listing[] lists;
   private int N;
   private int count;
  
  
   //constructor of defautl size,100
   public ListingArray() {
       N=100;
       lists=new Listing[N];
       count=0;
   }
   //constructor for user given size
   public ListingArray(int size) {
       lists=new Listing[size];
       N=size;
   }
   //Addng Listing at given index
   public void addListing(Listing l,int index) {
       lists[index]=l;
       count++;
   }
   //Method to print Listing objects
   public void showAll(){
       System.out.printf("%-20s%-10s ","Name","Age");
       System.out.printf("----------------------- ");
       for (int i = 0; i < count; i++) {
           System.out.println(lists[i].toString());
       }
   }
  

}

----------------------------------------------------------------------------

//Tester program
//ListingDriver.java
public class ListingDriver {
   public static void main(String[] args) {
      
       //create an instance of ListingArray of size,5
       ListingArray larray=new ListingArray(5);
      
       //calling addListing with Listing constructor
       larray.addListing(new Listing("mark", 21), 0);
       larray.addListing(new Listing("john", 22), 1);
       larray.addListing(new Listing("sundhar", 23), 2);
       larray.addListing(new Listing("heidi", 21), 3);
       //calling showAll
       larray.showAll();
      
   }

}

----------------------------------------------------------------------------

Sample Output:

Name                Age     
-----------------------
mark                21      
john                22      
sundhar             23      
heidi               21