Question

Write the following in Java Design an enum called Rating, it has the following names: bad,...

Write the following in Java

Design an enum called Rating, it has the following names: bad, okay, good, better, best


Design a class called Review (reviews might be for a restaurant, movie, etc), it has two attributes: a Rating type called rating, a String type called comment.


Design getters and setters for the attributes.


Override the toString() method to display both the rating and the comment.


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

For this, we create an enum Rating and put the given values in it.

Then in class Review, we create a rating variable of type Rating and comment variable of type string.

The using setter methods for each, we assign the values for both.

Then using getter methods we can retrieve their respective values .

Then we override the toString() method to return these values as string in order to print them.

In main() method, we create object for Review class and assign rating and comment here and pass them to respective setter methods.

And finally, print the object.

CODE:

import java.util.Scanner;

enum Rating //defining Rating enum
{
bad, okay, good, better, best;
}

class Review{
Rating rating;
String comment;
  
public void setRating(Rating rt){
this.rating = rt;
}
  
public void setComment(String rv){
this.comment = rv;
}
  
public Rating getRating(){
return rating;
}
  
public String getComment(){
return comment;
}
  
public String toString(){ //overriding toString() here
return getRating().toString()+":\t"+getComment(); //returning after converting to String
}
}

public class Main
{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Rating rtg = null; //initializing rating as null
       Review rvw = new Review();
       System.out.println("Chose the rating for the restaurant food:\n1.bad \n2.okay\n3.good\n4.better\n5.best");
       int choice = sc.nextInt();
      
       switch(choice){ //assigning rating to
       case 1: rtg = Rating.bad;
       break;
       case 2: rtg = Rating.okay;
       break;
       case 3: rtg = Rating.good;
       break;
       case 4: rtg = Rating.better;
       break;
       case 5: rtg = Rating.best;
       break;
      
       }
       rvw.setRating(rtg); //setting the rating
           System.out.print("Write a short comment: ");
           sc.skip("[\r\n]+"); //getting rid of new line character
           String str = sc.nextLine();
           rvw.setComment(str); //seeting review
           System.out.println("Rating \tComment");
       System.out.println(rvw); //priting the rating and review
   }
}

OUTPUT:

input Chose the rating for the restaurant food: 1.bad 2.okay 3.good 4.better 5.best 3 Write a short comment: Liked the food.

  

Add a comment
Know the answer?
Add Answer to:
Write the following in Java Design an enum called Rating, it has the following names: bad,...
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
  • Write the following in Java Design a class called ReviewSystem, this class should have an attribute...

    Write the following in Java Design a class called ReviewSystem, this class should have an attribute reviewList, which is an ArrayList type. add a constructor to initialize the attribute. add a method addReviewAndComment(). This method asks from the user to provide an integer (0 to 4 for the five ratings) and a String as a comment. If the user inputs -1, it ends the loop of asking. All valid user inputs should be saved to the reviewList attribute. For example:...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • You must write C# classes which are used to manage product review data for Books and...

    You must write C# classes which are used to manage product review data for Books and Cars. You will write an abstract class called Review which abstracts common properties (review rating and comments). You will then create an Interface responsible for the functionality to display the Reviewcontent. Then, you will create 2 concrete classes which inherit from Review and implement the Interface. Finally, you will create console applicationwhich will create 2 CarReview instances and 2 BookReview instances and display them...

  • 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....

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava...

    no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava a Create the "middle" of the diagram, the DVD and ForeignDVD classes as below: The DVD class will have the following attributes and behaviors: Attributes (in addition to those inherited from Medialtem): • director:String • rating: String • runtime: int (this will represent the number of minutes) Behaviors (methods) • constructors (at least 3): the default, the "overloaded" as we know it, passing in...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

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