Question

import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

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

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


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:

Add a comment
Know the answer?
Add Answer to:
import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...
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
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    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)    {...

    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,...

    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...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

  • import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    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)...

    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');...

  • Please write this C# code- Your main program will use a class named Coin, which has...

    Please write this C# code- Your main program will use a class named Coin, which has the following field: • A private String named SideUp. The SideUp field will hold either “heads” or “tails” indicating the side of the coin that is facing up. The Coin class has the following methods: • A no-arg constructor that randomly determines the side of the coin that is facing up (“heads” or “tails”) and initializes the SideUp field accordingly. • A void method...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

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