import java.util.Scanner;
public class Client{
public static void main(String args[]){
Coin quarter = new Coin(25);
Coin dime = new Coin(10);
Coin nickel = new Coin(5);
Scanner keyboard = new Scanner(System.in);
int i = 0;
int total = 0;
while(true){
i++;
System.out.println("Round " + i + ": ");
quarter.toss();
System.out.println("Quarter is " + quarter.getSideUp());
if(quarter.getSideUp() == "HEADS")
total = total + quarter.getValue();
dime.toss();
System.out.println("Dime is " + dime.getSideUp());
if(dime.getSideUp() == "HEADS")
total = total + dime.getValue();
nickel.toss();
System.out.println("Nickel is " + nickel.getSideUp());
if(nickel.getSideUp() == "HEADS")
total = total + nickel.getValue();
System.out.println("Your total is now " + total);
if(total == 100){
System.out.println("Congratulations, you WIN!");
break;
}
if (total > 100){
System.out.println("Sorry, you lose!");
break;
}
System.out.print("Another round? (Y or N): ");
char another = keyboard.next().charAt(0);
another = Character.toUpperCase(another);
if(another == 'Y'){
continue;
}
if(another == 'N'){
System.out.println("Goodbye! Sorry you don't like the
game.");
break;
}
}
}
}
import java.util.Random;
public class Coin{
int value;
String sideUp;
public Coin(int value){
if(value == 25 || value == 10 || value == 5){
value = value;
}
else
value = 1;
}
public void toss(){
Random rand = new Random();
int side = rand.nextInt(2);
if(side == 1){
sideUp = "HEADS";
}
if(side == 0){
sideUp = "TAILS";
}
}
public String getSideUp(){
return sideUp;
}
public int getValue(){
return value;
}
}
When the program is ran the value is not returned so 'total' stays at 0 and I can't figure out why
The output and screenshot is at end of this answer.
I have made sure to include all that is necessary for you to
complete this assignment.
In case, if something is missing, drop me a comment.
If you have any queries, need changes in the code or need help
in following the code, leave me a comment.
I will reply within reasonable time.
If this answer helps you with your assignment, do upvote
it.
Your vote will motivate me to work better.
Here is the code.
//File: Client.java (no change)
import java.util.Scanner;
public class Client{
public static void main(String args[]){
Coin quarter = new Coin(25);
Coin dime = new Coin(10);
Coin nickel = new Coin(5);
Scanner keyboard = new Scanner(System.in);
int i = 0;
int total = 0;
while(true){
i++;
System.out.println("Round " + i + ": ");
quarter.toss();
System.out.println("Quarter is " + quarter.getSideUp());
if(quarter.getSideUp() == "HEADS")
total = total + quarter.getValue();
dime.toss();
System.out.println("Dime is " + dime.getSideUp());
if(dime.getSideUp() == "HEADS")
total = total + dime.getValue();
nickel.toss();
System.out.println("Nickel is " + nickel.getSideUp());
if(nickel.getSideUp() == "HEADS")
total = total + nickel.getValue();
System.out.println("Your total is now " + total);
if(total == 100){
System.out.println("Congratulations, you WIN!");
break;
}
if (total > 100){
System.out.println("Sorry, you lose!");
break;
}
System.out.print("Another round? (Y or N): ");
char another = keyboard.next().charAt(0);
another = Character.toUpperCase(another);
if(another == 'Y'){
continue;
}
if(another == 'N'){
System.out.println("Goodbye! Sorry you don't like the
game.");
break;
}
}
}
}
//File: Coin.java
import java.util.Random;
public class Coin{
int value;
String sideUp;
public Coin(int value){
if(value == 25 || value == 10 || value == 5){
this.value = value; //replaced value = value with
this.value = value;
}
else
value = 1;
}
public void toss(){
Random rand = new Random();
int side = rand.nextInt(2);
if(side == 1){
sideUp = "HEADS";
}
if(side == 0){
sideUp = "TAILS";
}
}
public String getSideUp(){
return sideUp;
}
public int getValue(){
return value;
}
}
And here is the output when you execute the code
//Output:
import java.util.Scanner; public class Client{ public static void main(String args[]){ Coin quarter = new Coin(25);...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 { public static void main(String[] args) { String name; String answer; int correct = 0; int incorrect = 0; Scanner phantom = new Scanner(System.in); System.out.println("Hello, What is your name?"); name = phantom.nextLine(); System.out.println("Welcome " + name + "!\n"); System.out.println("My name is Danielle Brandt. " +"This is a quiz program that...
import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn); else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...
import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //ask the user for a temperature System.out.println("Enter a temperature:"); double temp = scnr.nextDouble(); //ask the user for the scale of the temperature System.out.println("Is that Fahrenheit (F) or Celsius (C)?"); char choice = scnr.next().charAt(0); if(choice == 'F') { //convert to Celsius if given temperature was Fahrenheit System.out.println(temp + " degrees Fahrenheit is " + ((5.0/9) * (temp-32)) + " degrees Celsius"); } else {...
import java.util.Scanner; import java.io.File; public class Exception2 { public static void main(String[] args) { int total = 0; int num = 0; File myFile = null; Scanner inputFile = null; myFile = new File("inFile.txt"); inputFile = new Scanner(myFile); while (inputFile.hasNext()) { num = inputFile.nextInt(); total += num; } System.out.println("The total value is " + total); } } /* In the first program, the Scanner may throw an...
please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
# JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...