Question

Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

Consider the Automobile class:

public class Automobile {
   private String model;
   private int rating; // a number 1, 2, 3, 4, 5
   private boolean isTruck;

   public Automobile(String model, boolean isTruck) {
      this.model = model;
      this.rating = 0; // unrated
      this.isTruck = isTruck;
   }
   
   public Automobile(String model, int rating, boolean isTruck) {
      this.model = model;
      this.rating = rating;
      this.isTruck = isTruck;
   }
          
    public String getModel() {  
      return model;
   }

   public int getRating() {  
      return rating;
   }
   
   public boolean isTruck() {  
      return isTruck;
   }

   private void updateRating(int newRating) {                           
      rating = newRating;
   }
   
   public void print() {                           
      System.out.println(model + ", rating: " + rating+", is a truck? : "+isTruck);
   }
}//end class

Write a public static method named getUnratedAutomobiles. This method takes as a parameter an ArrayList called automobiles that contains Automobile objects. It returns an ArrayList that contains only the titles of all unrated Automobile objects on the automobiles ArrayList. Assume the ArrayList has size >=0.

If there are no unrated Automobiles, the method returns an empty ArrayList.

Example- after these statements execute:

    ArrayList<Automobile> automobiles = new ArrayList<Automobile>();

    automobiles.add(new Automobile("Big Yellow", 5, False));

    automobiles.add(new Automobile("Centaur", 2, True));

    automobiles.add(new Automobile("Speedster", False));

    automobiles.add(new Automobile("Slow Steady", 3, False));

    automobiles.add(new Automobile("Beast", True));

    ArrayList<String> unratedTitles = getUnratedAutomobiles(automobiles);

The ArrayList unratedTitles contains "Speedster" and “Beast” as these are unrated (have no ratings).

Write the getUnratedAutomobiles method definition below:

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

Answer:

Here is the code:

It takes the arraylist of automobile objects, then traverses the arraylist using a for loop.

If any automobile object has rating equal to 0, it's model is added to the new result arraylist.

FInally, result arraylist is returned.

Code:


public static ArrayList<String> getUnratedAutomobiles(ArrayList<Automobile> automobiles)
{
ArrayList<String> result = new ArrayList<String>();
  
for(int i=0; i<automobiles.size(); i++)
{
if(automobiles.get(i).getRating()==0)
{
result.add(automobiles.get(i).getModel());
}
}
  
return result;
}

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Consider the Automobile class: public class Automobile {    private String model;    private int rating;...
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
  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);     

    public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);               private int minAge;               private Rating(int i)        {              minAge = i;        }               public int getMinAge()        {              return minAge;        }               public void setMinAge(int age)        {              minAge = age;        }               public String toString()        {              switch(this)              {              case GENERAL:                     return "G";              case PARENTALGUIDANCE:                     return "P";              case MATURE:                     return...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...

    public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } } public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers;    // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; }    // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers];    for(int i=0;i<castMembers.length;i++)...

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