Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Change Calculator

$
0
0
I am currently trying to figure out how to print the amount of change to be given in another class, but I'm having a bit of difficulty since the instructor wants us to code it with a while loop. Here are my two codes for the assignment.

class ChangeCalculator{
  private int changeValue;
  private int numQuarters;
  private int numDimes;
  private int numNickels;
  private int numPennies;
  
  // Constructor
  public ChangeCalculator(int value){
    this.changeValue = value;
    this.numQuarters = -1;
    this.numDimes = -1;
    this.numNickels = -1;
    this.numPennies = -1;
  }
  
  // Queries to retrieve instance variable values
  public int getNumQuarters(){
    return this.numQuarters;
  }
  
  public int getNumDimes(){
    return this.numDimes;
  }
  
  public int getNumNickels(){
    return this.numNickels;
  }
  
  public int getNumPennies(){
    return this.numPennies;
  }
  
  public void calculateChange(){
    while (this.changeValue <= 25){
      this.changeValue = (this.changeValue - 25);
      ++this.numQuarters;
    }
    
    while (this.changeValue <= 10){
      this.changeValue = (this.changeValue - 10);
      ++this.numDimes;
    }
    
    while (this.changeValue <= 5){
      this.changeValue = (this.changeValue - 5);
      ++this.numNickels;
    }
    
    while (this.changeValue <= 1){
      this.changeValue = (this.changeValue - 1);
      ++this.numPennies;
    }
  }
}


import java.util.Scanner;

class Change{ 
  public static void main(String[] args){    
    System.out.println("How much change?:");
    Scanner in = new Scanner(System.in);
    int num = in.nextInt();
    System.out.println("Your input is " + num + ".");
    
    System.out.println("Number of quarters: ");
    System.out.println("Number of dimes: ");
    System.out.println("Number of nickels: ");
    System.out.println("Number of pennies: ");
  }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>