• Home
  • Blog
  • Prompts and Reads Integer Values Program

Prompts and Reads Integer Values Program

0 comments

QUESTIONS

  1. Write a program that prints the following hourglass shape. Use a single System.out.print() statement to generate the whole display, and do not print any unneeded characters. (That is, don’t make any character string longer than it has to be.)
    ##########
     ########
      ######
       ####
        ##
       ####
      ######
     ########
    ##########
    
  2. Write a program that prompts for and reads integer values for typing speed and number of characters in the document, then prints the time required to type them as a floating point result.
  3. Write a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialise instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “Person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a driver class called DogMain, whose main method instantiates and updates several Dog objects. You may use Dog instead of DogMain as your driver class if you wish.
  4. Write a program that plays the Hi-Lo guessig game with numbers. The program should pick a random number between 11 (inclusive) and 88 (exclusive), then repeatedly prompt the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or choose to quit. Use a sentinel value to determine whether the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing games until the user chooses to stop. You are required to utilise at least a while loop and a for loop correctly. We note that PalindromeTester.java (see Listing 5.9 of the textbook) and Guessing.java (see Listing 5.5 of the textbook) can be of some assistance.A general strategy to facilate your testing when random numbers are involved is to display those numbers at appropriate times during the testing. For your practice, you are required to prompt/display the expected correct guess, within a pair of square brackets, right at the time of your being about to enter your guess so that you can pretend to guess correctly or incorrectly or in whatever way that would suit your testing purpose. You are thus required to leave this prompt mechanism within your submitted program even though the final “software” would be expected to have this prompt mechanism removed in order to make it a meaningful game.
    • Create a Card class so that at least
      • it contains an integer instance variable cardValue that represents the corresponding value on the card with 1 ≤ cardValue ≤ 52;
      • the constructor will initiate the cardValue automatically to a random number between 1 and 52 (inclusive on both sides);
      • it contains a method to get the card value;
      • it contains a method to set a new value to the card.

      The design of the Card class is likely to be somewhat similar to the Die class defined in Chapter 4.

    • Then write a class called PairOfCards, composed of two Card objects. Include methods to set and get the individual card values, a method to randomise the cards, and a method that returns the current sum of the two card values. Include a static main method in PairOfCards so that it can instantiate and use a PairOfCards object to illustrate all the features/methods in the class.
  5. Write a menu-driven Java program Calculator.java. When you execute the program, it should first display a simple menu, similar to something like
    SIMPLE CALCULATOR
    Written by YOUR NAME HERE.
    
    1. Input new X and Y
    2. X + Y
    3. X - Y
    4. X * Y
    5. X / Y
    0. Exit
    
    Your choice -> 
    

    The numbers X and Y will be automatically initialised to 0, and they can be modified anytime one wants. When you select, say, 2, then the program will calculate the sum of X and Y, and report the result in the form of

    12.3 + 45.6 = 57.9
    

    if X is 12.3 and Y is 45.6. The other actions should be similarly and accurately reported. The execution of the program will carry on until 0 is entered as a choice.

The answer should contain all the Java source code, in files directly compilable, and a Microsoft Word document that, question by question, list the Java source code (not as screen shots) as well as the screen shots of the corresponding execution result. Please note that the screen shots must be easily readable when printed out.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}