Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

The Coins Class

Objectives
•Use methods.
•Use local variables.
•Use arithmetic expressions.
•Use Scanner to input values.
•Use a class constant.
Hand-in Requirements 
All projects and laboratories will be submitted electronically through Blackboard. Zip up your entire lab directory to submit as the source. (Right click on the lab folder and follow Send To > Compressed (zipped) Folder or 7-Zip > Add to "lab2.zip"). The lab folder should include the following:
•Coins.java
•CoinsOutput.txt

Tasks

prepare a program that prints 

Lab 2 written by YOURNAME and calls two methods: 
1.Input the number of quarters, dimes, nickels, and pennies from the user. Print out the number of coins and total value in dollars.
2.Input the number of cents from the user. Determine and print out the number of quarters, dimes, nickels, and pennies to add up to that number of cents. No, you can't use all pennies.

Details

Getting Input from the User

You can get input from the user using a Scanner object. Here is an ex program with key lines in bold.

import java.util.*;

public class HalfDollar {
public static final Scanner CONSOLE = new Scanner(System.in);

public static void main(String[] args) {
halfDollarAmount( );
}

public static void halfDollarAmount( ) {
System.out.print("Enter the number of half dollars: ");
int halfDollar = CONSOLE.nextInt( );
double amount = halfDollar * 0.50;
System.out.println(halfDollar + " half dollars is $" + amount);
}
}

The import statement tells Java that we want to use Java's java.util package (Scanner is part of this package).

The statement: 

public static final Scanner CONSOLE = new Scanner(System.in);
declares a special kind of variable (a class constant) named CONSOLE of type Scanner and stores an "object" for input from the keyboard in CONSOLE. CONSOLE can be used anywhere in the class.

The first two statements of the halfDollarAmount method: 
1.prompt the user for information, and
2.input a number from the keyboard and stores the number in a variable named halfDollar.

You only need the declaration/assignment for CONSOLE once in the class. On the other hand, you need prompt and input statements for each value you want the user to enter.

Local Variables

You should have local variables for each value that is entered and each value that you find out. For ex, for the first method there should be a local variable that stores the total dollar amount of the change, as well as a local variable for the total number of coins.

Making Change

For the second method, we can use integer division and the mod operator to make change. Suppose the user enters 99 as the number of cents. The sequence of calculations should be as follows.
1.The integer division 99 / 25 can be used to determine the number of quarters (3).
2.Using the mod operator 99 % 25 determines the remaining number of cents to be converted into change (24).
3.Integer division 24 / 10 can be used again to determine the number of dimes (2).
4.The mod operator 24 % 10 can be used again to determine the remaining amount (4).
5.Next, for nickels, integer division 4 / 5 for the number of nickels (0) and a mod operation 4 % 5 for the remaining amount (4).
6.Whatever is left is the number of pennies (4).

Of course the values that were find outd (3, 24, 2, 4, 0, 4) would be different if the user enters something different from 99. This means we should have variables to store all of these values. For ex, we might have a variable named cents to hold the value that the user entered, a variable named remainingAfterQuarters for the remaining amount after the quarters are determined, which could be assigned by:

int remainingAfterQuarters = cents % 25;

Output File

Include the output of your program in a file named CoinsOutput.txt. 

Rubric
Your program should compile without any errors. A program with more than one or two compile errors will likely get a zero for the whole assignment.

The following criteria will also be used to determine the grade for this assignment:
• If the main method of your program prints "Lab 2 written by [...]".
• If your submission was a Zip file named lab2.zip containing a folder named lab2, which contains the other files.
• If your Java program was in a file named Coins.java.
• If the output of your Java program was in a file named CoinsOutput.txt.
• If your program contains a comment that describes what the program does and contains a comment for each method.
• If your program is indented properly.
• If your main method calls a method that does the following:
? If it prompts the user for the number of quarters, dimes, nickels, and pennies.
? If it prints out the total number of coins and their value in dollars.
• If your main method calls another method that does the following:
? If it prompts the user for the number of cents.
? If it prints out the number of quarters, dimes, nickels, and pennies necessary to add up to that number. 

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M998859

Have any Question?


Related Questions in Programming Language

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

Question 1 what is hadoop explaining hadoop 2 what is

Question: 1. What is Hadoop (Explaining Hadoop) ? 2. What is HDFS? 3. What is YARN (Yet Another Resource Negotiator)? The response must be typed, single spaced, must be in times new roman font (size 12) and must follow t ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As