View the step-by-step solution to:

a file defining a new class, representing a new data type; and a file containing main() and other methods for interacting with the user (this is the...

  • a file defining a new class, representing a new data type; and
  • a file containing main() and other methods for interacting with the user (this is the program you will actually run).

At a high level your program will look like this (but the names of your files will be tailored to suit your chosen option and with a lot more comments!):

import java.util.Scanner;

public class MainProgram {

   //method(s) for reading and validating

   //user input and creating a new DataType

   public static void main(String[] args) {

       Scanner sc = new Scanner(;

       DataType data;

       //calls method to read details from user

       //and assigns result to data

       //displays data using println



public class DataType {

   //variable declarations

   //a constructor

   //getters and setters

   public String toString() {

       //creates and returns a String

       //(does _not_ call println)



  1. new program (a source file containing a main() method) and have it import java.util.Scanner at the top. Give your program a meaningful name based on your selected option.
  2. new class to represent your new data type (this will be in its own .java file):
  3. Declare the instance variables (properties) at the top, making each one as private (as in private int a; //represents something).
  4. Define a constructor that has one parameter for each instance variable; the body of the constructor should copy the parameter values into their corresponding instance variables.
  5. Define getters and setters for each property, following the templates in the notes.
  6. Define a toString() method that displays the data in the object as specified in your chosen option. The implementation details of this vary depending on the option you choose. Remember that you can a String value one piece at a time and use += to append to it.
  7. Add some temporary code in the main program that declares then instantiates (with new) an object of your new data type with hard-coded values, and then display the object. This can verify that the constructor and toString() work as intended.

verified it works, delete or comment out the temporary testing code.

  1. In the source file for your main program, create the method to read in the necessary details to your new data object. This method will have a single parameter of type Scanner so that main() can provide it with the Scanner to read from the user. This method will read in data from the user (storing it in local variables) and then a new instance of your data class and return it to main().
  2. In that read data method, before you create the new object, you will need to validate one or more user inputs. Use a while loop for this: show an error message while the user has not entered a valid value, and continue to ask them to enter the data until it is valid.

Using the names from the example high-level description above, the general structure of this method will be:

public static DataType readDataType(Scanner in) {

   //Local variables for each property of DataType

   //1. Interaction with the user to read valid values into those (may involve a while loop)

   //2. Creation of a new instance of DataType by passing those variables to its constructor

   return /* the new object */;


  1. In main(), call your read data method and store the result in a variable of your new data type, then display that variable using System.out.println().
  2. That should make your program complete. Be sure to test it to make sure that it works as expected, then take a screenshot and submit the two source files to the submission folder on MyLO.

The five different programs you can choose from are:

▽ Option 1: Sensor Readings

a program that will read values from physical sensors (temperature, humidity, light, etc.) which could then be used to control other devices. At this stage, the program will allow the user to enter sensor details manually, and will print the values back to the screen.

This program will use a Reading class, which you will create, to store and work with sensor readings. Each Reading has an annotation (a String), a reading id (an int) and and a value (a double). These become its instance variables.

The program must include the following:

  • In Reading, instance variable declarations, a constructor to initialise their values, getters and setters for each, and a toString method.
  • In the main program, A readReading method that reads in a sensor reading from the user, instantiates a new Reading object using those details and returns that object to the calling code in main(). Before creating the Reading object it must ensure there is a meaningful annotation provided (its length is at least 15), asking the user to re-enter the value while not valid.
  • Reading's toString() method should return a formatted String according to the following pattern, where italicised values refer to those variables in the object: 
  • "#idvalue (annotation)" followed by "check sensor" if the value is 0, or "check reading" if value is larger than 100 
  • For example, a particular Reading object may appear as:

#1138: 0 (my shiny internet-connected toaster) check sensor

  • main() should declare a Reading variable, and then perform the following steps:
  1. Print the message "- Enter Reading Data -"
  2. Use readReading to read in details from the user to instantiate a Reading object, and then store it in the local variable.
  3. Print the message "- You Entered -"
  4. Print the Reading using System.out.println() (it does not need to manually call toString).

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