Question

Please write in ECLIPSE JAVA. Please no buffered readers and please do not use Java’s built...

Please write in ECLIPSE JAVA. Please no buffered readers and please do not use Java’s built in data structures or data functionality (Like sorting and searching), however stuff like java.util.scanner and wild cards like java.io.* and java.util.* are fine, but please remember I am very much a beginner at this and would much rather understand the material instead of doing it the easier way using built-in data structures. It would also be nice if you could do it in the way that is suggested because I understand that way pretty well, however it's not critical. Thanks in advance.

Objective:

Write a program which emulates the final game of a famous price related game show. The program must read from a file (prizeList.txt) and populate an array of 50 prizes. Each item has an associate name and price, which is in the file and also in the order named. The name and price are separated by a tab. The program must then randomly pick 5 items out of that array for the showcase showdown, in which repeated items are allowed. Then, the program must prompt the user with the names (individual prices hidden) of the prizes that have been randomly selected and ask them to enter the total cost closest without going over. Since there is not another contestant in this version of the game, the program must also check to see if the price guessed is within $2,000 of the actual retail price or else they lose as well. The game will continue until the user chooses to quit.

Link to prizeList https://cse.sc.edu/~shephejj/csce146/Homework/ShowCaseShowDownFiles/prizeList.txt

Suggested Methodology

You can solve this in any number of ways, and here’s a way you may take to approach this problem.

  • 3 Classes
    • Prize: This is a simple class which holds a single item from the list provided. As such it has two instance variables price and name, and also all the accessors, mutators, and constructors associated with it.
    • Showcase: A more complex class which holds the arrays for the entire prize list and the randomly prize array that is the showcase. Each array uses the type Prize. This class must populate the entire prize array upon its construction from the file. Also the file contains each prize name and cost separated by a tab. A method for populating the showcase by randomly selecting items from the prize list is strongly recommended.
    • ShowcaseGame: This is the entire front end of the game. All of the users input and the prompts should go in this class which contains nothing more than a main method.

Example Dialog:

Welcome to the showcase show down!

Your prizes are:

milk

bread

car

car

car

You must guess the total cost of all without going over

Enter your guess

60000

You guessed 60000.0 the actual price is 60012.0

Your guess was under! You win!

Would you like to play again? Enter 'no' to quit

yes

Welcome to the showcase show down!

Your prizes are:

moose

boat

bread

bread

bread

You must guess the total cost of all without going over

Enter your guess

40000

You guessed 40000.0 the actual price is 51021.0

I'm sorry but that guess was bad. You lose for being bad.

Would you like to play again? Enter 'no' to quit

yes

Welcome to the showcase show down!

Your prizes are:

milk

moose

cheese

cheese

boat

You must guess the total cost of all without going over

Enter your guess

200000

You guessed 200000.0 the actual price is 51015.0

I'm sorry but that was over... You get nothing

Would you like to play again? Enter 'no' to quit

no

Goodbye

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

Prize.java

public class Prize {
   // Data members
   private String name;
   private double price;

   // Constructor to initialize data members
   public Prize(String name, double price) {
       this.name = name;
       this.price = price;
   }

   // Accessor and Mutators for data members
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }
}

Showcase.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;

public class Showcase {
   // Create 2 arrays of type Prize
   private Prize prizeList[];
   private Prize showcaseList[];

   // Constructor to initialize data members
   public Showcase() {
       prizeList = new Prize[51];
       showcaseList = new Prize[5];
       // Call this to populate prizeList
       buildPrizeList();
   }

   public void buildPrizeList() {
       // Read file using scanner
       File file = new File("prizeList.txt");
       try {
           Scanner scan = new Scanner(file);
           String line = "";

           // This variable keeps track of index of array
           int index = 0;

           // Iterate while loop till file has data
           while (scan.hasNext()) {
               // Read line using scan, split it by delimiter tab \t
               // data[0] will contain name and data[1] will contain prize
               line = scan.nextLine();
               String data[] = line.split("\t+");
               // Store Prize object in array by passing arguments
               prizeList[index] = new Prize(data[0], Double.parseDouble(data[1]));
               // Increment index by 1 to point to nex location
               index++;
           }
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
   }

   public void buildShowcaseList() {
       Random rand = new Random();
       // Iterate loop 5 time and generate 5 random indexes
       // and store corresponding prize in shoecaseList
       for (int i = 0; i < 5; i++) {
           int randIndex = rand.nextInt(51);
           showcaseList[i] = prizeList[randIndex];
       }
   }

   // This method returns showcaseList
   public Prize[] getShowcaseList() {
       return showcaseList;
   }

   // This method returns prizeList
   public Prize[] getPrizeList() {
       return prizeList;
   }
}

ShowcaseGame.java

import java.util.Scanner;

public class ShowcaseGame {

   public static void main(String[] args) {
       String filename;
       Scanner scan = new Scanner(System.in);

       // Create a Showcase object
       Showcase showcase = new Showcase();
       System.out.println("Welcome to the showcase show down!");
       String choice = "";
       double guessedPrice = 0;
       double actualPrice = 0;

       // Iterate loop user enters no
       while (!choice.equalsIgnoreCase("no")) {
           // Build showcase list and store in showcaseList by calling getter function
           showcase.buildShowcaseList();
           Prize showcaseList[] = showcase.getShowcaseList();

           // Iterate a loop over this list and print the prize name
           // add its price to variable actualPrice
           System.out.println("Your prizes are:");
           for (int i = 0; i < showcaseList.length; i++) {
               System.out.println(showcaseList[i].getName());
               actualPrice += showcaseList[i].getPrice();
           }

           // ASk user to guess price
           System.out.println("\nYou must guess the total cost of all without going over.");
           System.out.print("Enter your guess: ");
           guessedPrice = Double.parseDouble(scan.nextLine());

           // Print guessed prize and actual price
           System.out.println("You guessed " + guessedPrice + ", the actual price is " + actualPrice);

           // If the difference between prices is within 2000
           // User won!
           if (Math.abs(guessedPrice - actualPrice) <= 2000) {
               System.out.println("Your guess was under! You win!");
           }
           // Else print messages accordingly
           else if (guessedPrice < actualPrice) {
               System.out.println("I'm sorry but that guess was bad. You lose for being bad.");
           } else {
               System.out.println("I'm sorry but that was over... You get nothing");
           }

           // Ask if they want to play again
           System.out.print("\nWould you like to play again? Enter 'no' to quit: ");
           choice = scan.nextLine();

           // Re-set actual price to 0
           actualPrice = 0;
           System.out.println();
       }
       System.out.println("Goodbye");
   }

}

OUTPUT

NOTE - Make sure the input file prizeList.txt is properly formatted, name and price are separated by tab otherwise exception will be raised

Add a comment
Know the answer?
Add Answer to:
Please write in ECLIPSE JAVA. Please no buffered readers and please do not use Java’s built...
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
  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • – Palindrome Game Please write a Java program to verify whether a given word is a...

    – Palindrome Game Please write a Java program to verify whether a given word is a palindrome. You must stop your program when the user enters ‘@’ character as the word. You may write a recursive program to solve this game, but you don’t have to. You may use a stack and/or a queue to solve this game, but you don’t have to. You must run 3 test cases for your program.   Your test case #1 must look as follows:...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • Please do it by C++ programming! By completing this project, you will demonstrate your understanding of:...

    Please do it by C++ programming! By completing this project, you will demonstrate your understanding of: 1) Arithmetic operations (addition, subtraction, multiplication, division, modulus) 2) Conditional statements (If, If-Else, If-ElseIf-Else, Conditional Operator, Switch) 3) Precondition, postcondition, and indexing loops (Do-While, While, For) 4) Standard text file operations (open, close, read, write) 5) Modularizing code by breaking into functions (including functions with input parameters, and ones that return a specific data type) There is a number guessing game similar to MasterMind...

  • Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

    Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X'). Write a function called guessLetter that...

  • JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...

    JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to...

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