Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pleasem I need help with this code: It is supposed to read 2 text files and play a price is right game. If anyone can

Pleasem I need help with this code: It is supposed to read 2 text files and play a price is right game. If anyone can help that would be amazing.

import java.io.*; import java.util.*;

class PriceIsRight2 { public static void main(String[] args) { Test obj = new Test(); obj.start(); } public void start() { String []names = new String[20]; String []items = new String[20]; String []players = new String[4]; double []bids = new double[4]; String item, choice="yes"; double price=0; Scanner s = new Scanner(System.in); names = loadNames(); items = loadItems(); System.out.println("-----jGRASP exec: java PriceIsRight2 "); System.out.println("You are about to play price is right game"); //kindly write description on your own as i do not know how your instructor expects it.... while(countNames(names)>0) { players = selectPlayers(names); while(choice.equalsIgnoreCase("yes")) { displayPlayers(players); item = selectItem(items); for(int i=0;i { String []vals = items[i].split("\t"); if(item.equals(vals[1])) { price = Double.parseDouble(vals[0]); } } removeItem(item,items); bids = getBids(players,item); int index = priceIsRight(bids,price); if(index != -1) { displayWinner(players, bids, price,index); } int obcount = overBidCount(bids,price); //getting the count of over bids if(obcount!=0) {//if there are over bids System.out.println(" Some of the bids were over the actual price!!!!"); System.out.println(" Wait, I am replacing the following players: ");

if(countNames(names)>obcount) {//checking whether the list has enough players to replace players = replacePlayers(names,players,bids,price); displayPlayers(players); } } else { System.out.println(" All of the bids were below the actual price same players will play again:" ); } System.out.println(" Do you want to keep playing: (Ex: yes)"); choice = s.nextLine(); } } }

/*Function to count the names left in the list*/ public int countNames(String []names) { int c=0; for(int i=0;i { if(!names[i].equals("")) {//counting non-empty indexes of the names array c++; } } return c; //returning the count names in the list }

/*Function to count the no. of over bids*/ public int overBidCount(double []bids, double price) { int c = 0; for(int i=0;i { if(bids[i]>price) { // calculating the number of over bids c++; } } return c; //returing over bids count }

/*Function to replace players in place of over bid players*/ public String [] replacePlayers(String []names, String []players, double []bids, double price) { for(int i=0;i { if(bids[i]>price) { //if a player's bid is higher than that of original price int flag = 0; System.out.println(players[i]); while(flag==0) { int c = 0; int pi = (int)(Math.random()*20); //generates number between 0 to 20 for(int k=0;k { if(names[pi].equals(players[k]) || names[pi].equals(players[i]) || names[pi].equals("")) {//trying to check whether this player already exists in the players list c++; } } if(c == 0) { //replacing the player and removing him from the list players[i]=names[pi]; names[pi]=""; //replacing player name with "" in names array...deleting flag = 1; } } } } return players; //returning the updated players list after replacement }

/*Function to diplay the selected players list*/ public void displayPlayers(String []players) { System.out.println(" Here is the list of players: "); for(int i=0;i { System.out.println(players[i]); } }

/*Function to remove selected item from the list*/ public void removeItem(String item, String []items) { for(int i=0;i { String []vals = items[i].split("\t"); if(item == vals[1]) { items[i]=""; //replacing item's information with "" in items list } } }

/*Function to load Names from Names.txt file*/ public String[] loadNames() { int i=0; String []names = new String[20]; Scanner sc = null; File f = null; try { f = new File("Names.txt"); sc = new Scanner(f); while(sc.hasNext()) {//reading names from text file and storing them in array names[i++]=sc.next(); } } catch (Exception e) { e.printStackTrace(); } return names; //returning names array }

/*Function to load items list from Items.txt file and store them in array*/ public String[] loadItems() { int i=0; String []items = new String[20]; Scanner sc = null; File f = null; try { f = new File("Items.txt"); sc = new Scanner(f); sc.useDelimiter(" "); while(sc.hasNext()) {//reading items from text file and storing them in array items[i++]=sc.next(); } } catch (Exception e) { e.printStackTrace(); } return items; }

/*Function to ramdomly select players from names list*/ public String[] selectPlayers(String []names) { int pIndexes [] = new int[4]; //array to store selected players' indexes from names array String players [] = new String[4]; int pi,flag; for(int i=0;i { flag = 0; pi = (int)(Math.random()*20); //generates number between 0 to 20...indexes for(int j=0;j { if(pi == pIndexes[j] || names[pi].equals("")) {//if the index(generated random number) is already exists or names[pi] is empty flag ++; } } if(flag == 0) {//storing the player name in players array and index in pIndexes array and replacing his/her name in names array with "" pIndexes[i] = pi; players[i] = names[pi]; names[pi]=""; i++; } } return players; //returning selected players array }

/*Function to select an item from the list*/ public String selectItem(String []items) { String item=""; int flag = 0; while(flag == 0) { int itemno = (int)(Math.random()*20); //generates number between 0 to 20...will be used as an index of items array while selecting item if(items[itemno]!="") { String []vals = items[itemno].split("\t"); item = vals[1]; flag = 1; } } return item; //returning the index of selected item }

/*Function to get bids from selected players*/ public double[] getBids(String []players, String item) { double []bids = new double[4]; Scanner s = new Scanner(System.in); System.out.println(" Item: "+item); for(int i=0; i { System.out.printf("%s%s",players[i]," , enter your bid: "); //System.out.println("---- enter your bid:"); bids[i] = Double.parseDouble(s.nextLine()); //storing player's bids in array } return bids; //returning players bids array }

/*Function to get the index of the player whose bid is nearer to the actual price of the item*/ public int priceIsRight(double []b, double price) { double v = 0; int index = -1; for(int i=0;i { if(b[i]>v && b[i] <=price) { v = b[i]; index = i; } } return index; //In general, returns the index of nearest bid player...returns -1 if all bids are greater than original price }

/*Function to display the winner */ public void displayWinner(String[]players, double []bids, double price, int index) { System.out.println("\tName\tBid --------------------------------- "); for(int i=0;i { //System.out.println(players[i]); } for(int i=0;i { System.out.printf("\t%s\t%f ",players[i],bids[i]); } System.out.println(" The actual price of the item is : "+price); System.out.println(" The winner is : "+players[index]); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions