View the step-by-step solution to:

defining a new enumerated and adding a property to your class of this type; defining (in the main program) a method to allow the user to select a


  • defining a new enumerated and adding a property to your class of this type;
  • defining (in the main program) a method to allow the user to select a value of this enumerated type from a menu; and
  • adding another method to your class to generate additional information not immediately apparent from its properties.

The high-level of your program will now be the following (where EnumType will have a specific name):

MainProgram.java

DataType.java

import java.util.Scanner;


public class MainProgram {


   //method(s) for reading and validating

   //user input, including DataType.EnumType,

   //and for creating a new DataType


   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       DataType data;


       //calls method to read details from user

       //and assigns result to data


       //displays data using println

   }

}

public class DataType {

   public enum EnumType { VALUES, GO, HERE };


   //variable declarations, now with one

   //of type EnumType


   //a constructor


   //getters and setters


   public String toString() {

       //creates and returns a String

       //(does _not_ call println)

   }

}

Here are the general steps to follow, with specific details given for each option (obviously, select the same option as you did in 6.1PP):

  1. Inside the class body for your data type, before the variable declarations, declare the new enumerated type.


the values of an enumerated type should be written in all UPPERCASE, because they are a kind of constant.

  1. Declare an additional instance variable with that type.
  2. Modify the constructor to take in a value of that and assign it to the variable.
  3. Add a getter and setter for the enumerated type property. The format is the same as for other types of data.
  4. Modify its toString() method to include the new variable.
  5. In the main program, add a method to read in a value of the enumerated type from the user. The easiest option for the user is to present them with a list of options and then read in an int. You can then use an if-else or switch construct to convert their selection into the appropriate value from the enumeration. (If you use switch then make the final case default: so that the compiler doesn't complain that your method might not return a value or that a variable holding its result might not have been initialised).

Note: Because the enumerated type is declared inside your class, when referring to it from your main program you will need to prefix the type name with the class's name (DataType.EnumType using the example above) and to refer to its values you will need to do the same (DataType.EnumType.GO using one of the example values above). This is a little tedious to type, but you will encounter briefer (but harder to understand) approaches later in your programming career.

Tip: If you have completed 4.3CR User Input Functions then consider copying the source code for promptForInt into your main program.

  1. Call that new method from your existing method for interacting with the user and creating the object of your class so you can obtain a value of the enumerated type to pass to its constructor.
  2. Add an additional method to your data class (details vary by option) which calculates information based on its internal state, and display the result of calling this from main().

The five options are the same as for 6.1PP:


▽ Option 1: Sensor Readings

  1. You will expand the Reading class so that it has an additional property representing the kind of sensor: Temperature, Humidity, and Light. In the source code for Reading, declare the enum Type with these values, making it public. Follow the instructions above to make use of the enumerated type.
  2. You will need to expand your Instrument Readings main program with another method to read a Reading.Kind value from the user, which should be called from the readReading method.
  3. Modify Reading's toString() to include its kind and return a String in the following format: 
  4. "kind #idvalue (annotation)" followed by "check sensor" if the value is 0, or "check reading" if value is larger than 100 
  5. For example, a particular Reading object may appear as:

HUMIDITY #1138: 0 (my internet-connected humidifier) check sensor

  1. Add a method to Reading according to the following specification to allow it to report its sensitivity (also known as 'resolution'), which varies by kind of sensor:


5. Method: double getSensitivity()

6. 

7. Returns:

8. double, the sensitivity of the sensor, depending on its kind

9. 

10.Variables:

11.double sensitivity, determined based on kind of sensor

12.

13.Steps:

14.1. Switch on value of kind

15.     case TEMPERATURE: assign sensitivity 0.1

16.     case HUMIDITY: assign sensitivity 0.5

17.     default: assign sensitivity 1 (assuming LIGHT)

18.2. Return sensitivity 

  1. In main(), after the code for printing the object, output an additional message, "Sensitivity: " followed by the result of calling getSensitivity() on the Reading object.

▽ Option 2: Cost Calculator

  1. You will expand the Expense class so that it has an additional property representing the kind of expense: Fixed, Flexible, and Discretionary. In the source code for Expense, declare the enum Kind with these values, making it public. Follow the instructions above to make use of the enumerated type.
  2. You will need to expand your Cost Calculator main program with another method to read an Expense.Kind value from the user, which should be called from the readExpense method.
  3. Modify Expense's toString() to include its kind and return a String in the following format: 
  4. "iddescription (kind) $cost" followed by "justification required" if the cost is larger than 500, or "authorisation required!" if cost is larger than 5000 
  5. For example, a particular Expense object may appear as:

234: Photocopier (DISCRETIONARY) $2100 justification required

  1. Add a method to Expense according to the following specification to allow it to calculate the amount of GST in the expense (the GST is 10%, so this method calculates one eleventh of the expense's cost):


5. Method: double getGST()

6. 

7. Returns:

8. double, the amount of the GST in the Expense's cost

9. 

10.Steps:

11.1. Evaluate and return cost/11 (ensuring result is a double)

  1. In main(), after the code for printing the object, output an additional message, "GST in expense: $" followed by the result of calling getGST() on the Expense object.

▽ Option 3: High Scores

  1. You will expand the GameScore class so that it has an additional property representing the game's difficulty: Normal, Hard, and Insane. In the source code for GameScore, declare the enum Difficulty with these values, making it public. Follow the instructions above to make use of the enumerated type.
  2. You will need to expand your High Scores main program with another method to read a GameScore.Difficulty value from the user, which should be called from the readGameScore method.
  3. Modify GameScores's toString() to include its difficulty and return a String in the following format: 
  4. "user namescore on game game id (difficulty)" followed by "Newb" if the value is less than 1000, or "Godlike" if value is larger than 999999 
  5. For example, a particular GameScore object may appear as:

bajo: 987 on game 256 (INSANE) Newb

  1. Add a method to GameScore according to the following specification to allow it to calculate a player's reputation points based on their score and level of difficulty:


5. Method: int getReputationPoints()

6. 

7. Returns:

8. int, the player's reputation score based on their game score and difficulty

9. 

10.Variables:

11.int reputation, determined based on difficulty and score

12.

13.Steps:

14.1. Switch on value of difficulty

15.     case NORMAL: assign reputation 1

16.     case HARD: assign reputation 2

17.     default: assign reputation 4 (assuming INSANE)

18.2. Evaluate and return reputation * score

  1. In main(), after the code for printing the object, output an additional message, "Reputation points earned: " followed by the result of calling getReputationPoints() on the GameScore object.

▽ Option 4: Casting Agent's Talent List

  1. You will expand the Actor class so that it has an additional property representing the actor's skill level: Wooden, Ham, or Oscar Worthy. In the source code for Actor, declare the enum Skill with these values, making it public. Follow the instructions above to make use of the enumerated type.
  2. You will need to expand your Casting Agent main program with another method to read an Actor.Skill value from the user, which should be called from the readActor method.
  3. Modify Actor's toString() to include its skill and return a String in the following format: 
  4. "skillname (member #id) $revenueM" followed by "Sharknado only" if the revenue is less than 5, or "A-list" if revenue is larger than 50 
  5. For example, a particular Actor object may appear as:

OSCAR_WORTHY: Hugh Jackman (member #24601), $68M A-list

  1. Add a method to Actor according to the following specification to allow it to calculate the commission to be paid to the agent (in dollars) if the actor is cast in a film, which we will assume is the fixed rate of 1% of the actor's revenue:


5. Method: int getCommission()

6. 

7. Returns:

8. double, the commission paid to an agent in dollars; 1% of revenue

9. 

10.Steps:

11.1. Evaluate and return revenue * 10000 (which is 1% of 1 million)

  1. In main(), after the code for printing the object, output an additional message, "Commission: $" followed by the result of calling getCommission() on the Actor object.

▽ Option 5: Make your own...

  1. If you are expanding your custom-designed program then consider what kind of categorical information you may wish to record in your data class. It should have at least three possible values. This then becomes your new enumerated type. Follow the instructions above to make use of the enumerated type.
  2. Expand your custom program to have another method to read in a value of your enumerated type. This should be called from your existing method for reading details from the user.
  3. Modify your class's toString to incorporate the instance variable of your enumerated type.
  4. Devise an additional method to calculate something for your data-oriented class. Look at the examples above for ideas.
  5. In main(), after the code for printing the object, output an additional message that includes the result of calling your new method.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question