Question

In: Computer Science

IN JAVA Question 1 The following classes along with the driver has been created and compiled....

IN JAVA

Question 1

The following classes along with the driver has been created and compiled.

public class Car 
{
    public void method1()
    {
     System.out.println("I am a car object");
    }  
}
class Point
{
    public void method2()
    {
       System.out.println("I am a Point object");
    }
}
class Animal
{
    public void method3()
    {
       System.out.println("I am an animal object");
    }
}

The following driver class has been created and all the lines of code inside the for loop is not compiling.
Modify the code and write it in the answer area so that it compiles and display the output. To get the full credit you cannot change anything else
 in the given code. you cannot change the code inside the above classes

class Driver
{
   public static void main(String[] args)
   {
     Object[] things = {new Car(), new Animal(),new Point()};//do not change this line
     
     for(int i = 0; i < things.length; i++)
     {
        things[i].method1();  //compiler error
        things[i].method2();  //compiler error
        things[i].method3();  //compiler error
     } 
   }
}

********************************************************************************************************************************************

Question 2

We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList

Content of the ArrayList before push

[alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte]

content of the ArrayList after the push method
[alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex]

import java.util.*;
class Person
{
   private String name;
   private String last;
   public Person(String name, String last)
   {
     this.name = name;
     this.last = last;
   }
   public String getLast()
   {
     return last;
   }
   public String getFirst()
   {
     return name;
   }
   public String toString()
   {
     return name + " " + last;
   }
}

public class ArrayList
{
  public static void main(String[] args)
  {
    ArrayList<Person> list = new ArrayList<Person>();
     list.add(new Person ("alex","Bus"));
     list.add(new Person("Mary", "Phillips"));
     list.add(new Person("Nik", "Lambard") );
     list.add(new Person("Rose","Rodd"));
     list.add(new Person("Esa","khan"));
     list.add(new Person("Jose","Martinex"));
     list.add(new Person("Nik","Patte"));
     System.out.println(list);
     push(list);
     System.out.println(list);

  
  }
//this method pushes all the people with the even length last name to the end of the list
 public static void push(ArrayList<Person> list) {
         
 }
     
   
}

Solutions

Expert Solution

Question 1

Class Driver should be as follows

class Driver
{
   public static void main(String[] args)
   {
     Object[] things = {new Car(), new Animal(),new Point()};//do not change this line
  
        // You must typecast the object type to its corresponding class type
        // You cannot use a for loop, as it may not throw a Compile time Exception 
        // but throws a Runtime Exception because method1() is called on the thing[0] 
        // and typecasted to Car class, which is not available in Animal and Point, 
        // So changing the Driver class as below, gives the desired output without any error.
        ((Car) things[0]).method1();  
        ((Animal) things[1]).method3(); 
        ((Point) things[2]).method2();
   }
}

OUTPUT

Question 2

//this method pushes all the people with the even length last name to the end of the list
        public static void push(ArrayList<Person1> list) {
                ArrayList<Person1> evenList = new ArrayList<Person1>();
        // Filtering only list items that has even lengthed last name
                evenList = (ArrayList<Person1>) list.stream().filter(action-> action.getLast().length()%2==0).collect(Collectors.toList());
                list.removeAll(evenList);
                list.addAll(evenList);
        }

There can be many more approaches as well to deal with this program, I have made use of streaming.

OUTPUT


Related Solutions

The following classes along with the driver has been created and compiled. public class Car {...
The following classes along with the driver has been created and compiled. public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify the code and write...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
Please include all classes including driver. IN JAVA Suppose that there is a big farm in...
Please include all classes including driver. IN JAVA Suppose that there is a big farm in California that supplies the majority of the Grocery stores in the twin cities with vegetables and fruits. The stores submit their orders and receive the shipment a week later. Based on the size of the orders, the farm management decides on the size of the truck to load. Create a Produce class that have an instance variable of type String for the name, appropriate...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class....
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class. You probably looped through the entire sequence one character at a time (using charAt() method of the String class), and check if the character is 'A', 'T', 'G', or 'C'. If it is not one of these four characters, the sequence contains an invalid base. Please re-create the isDNAvalid() method using a character pattern and the regular expression to validate the DNA sequence. previous...
For this question we will be using arrays and classes in Java to compute the min,...
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below: -Create methods for min, max, and average and call them from main to print out their values. -Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Company A is preparing its budget for the third quarter. The following information has been compiled:...
Company A is preparing its budget for the third quarter. The following information has been compiled: Cash collections $50,000 $40,000 $48,000 Cash payments Purchases of inventory 31,000 22,000 18,000 Operating expenses 12,000 9,000 11,600 Capital expenditures 13,000 25,000 0 The cash balance at the end of the second quarter is projected to be $4,000. The company is required to maintain cash balance of $5,000 and is authorized to borrow at the end of each month to make up any shortfalls....
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT