Java Program to Read Monetary Amount in Cents

2.14 Trouble: Counting Budgetary Units 47

When a char is cast into a numeric type, the character'southward Unicode is cast into the specified numeric type.

int i = ( int ) 'A' ; // the Unicode of graphic symbol A is assigned to i System.out.println(i); // i is 65

Implicit casting can exist used if the result of a casting fits into the target variable. Otherwise, explicit casting must be used. For example, since the Unicode of 'a' is 97, which is inside the range of a byte, these implicit castings are fine:

byte b = 'a'; int i = 'a';

Merely the post-obit casting is wrong, because the Unicode \uFFF4 cannot fit into a byte:

byte b = '\uFFF4';

To force assignment, use explicit casting, as follows:

byte b = (byte) '\uFFF4' ;

Any positive integer betwixt 0 and FFFF in hexadecimal can be cast into a grapheme implicitly. Any number not in this range must be cast into a char explicitly.

Note

All numeric operators can be applied to char operands. A char operand is automatically bandage into a number if the other operand is a number or a graphic symbol. If the other operand is a cord, the character is concatenated with the string. For instance, the following statements

int i = '2' + 'iii' ; // (int)'ii' is l and (int)'three' is 51 System.out.println( "i is " + i); // i is 101

int j = ii + 'a'; // (int)'a' is 97 Organization.out.println("j is " + j); // j is 99 System.out.println(j + " is the Unicode for grapheme "

+ (char)j);

System.out.println("Chapter " + '2');

display

i is 101 j is 99

99 is the Unicode for character c Chapter 2

Note

The Unicodes for lowercase letters are consecutive integers starting from the Unicode for 'a', and so for 'b', 'c', Á , and 'z'. The same is true for the uppercase letters. Furthermore, the Unicode for 'a' is greater than the Unicode for 'A'. So 'a' - 'A' is the same every bit 'b' - 'B'. For a lowercase letter ch, its corresponding majuscule letter is (char)('A' + (ch - 'a')).

ii.14 Problem: Counting Monetary Units

Suppose you want to develop a program that classifies a given amount of coin into smaller monetary units. The plan lets the user enter an amount as a double value representing a total in dollars and cents, and outputs a report list the monetary equivalent in dollars, quarters, dimes, nickels, and pennies, equally shown in the sample run.

Your program should study the maximum number of dollars, then the maximum number of quarters, and so on, in this gild.

numeric operators on characters

48 Chapter two Elementary Programming

Here are the steps in developing the programme:

1. Prompt the user to enter the amount as a decimal number, such as xi.56.

2.

Catechumen the corporeality (e.yard., 11.56) into cents (1156).

3. Separate the cents by 100 to notice the number of dollars. Obtain the remaining cents using

the cents rest 100.

4. Divide the remaining cents by 25 to discover the number of quarters. Obtain the remaining

cents using the remaining cents remainder 25.

5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining

cents using the remaining cents residual 10.

6. Divide the remaining cents by 5 to discover the number of nickels. Obtain the remaining

cents using the remaining cents remainder 5.

7. The remaining cents are the pennies.

viii.

Display the consequence.

The complete plan is given in Listing 2.ten.

L ISTING 2.10 ComputeChange.java

import class

1

import java.util.Scanner;

2

3

public course ComputeChange {

four

public static void main(String[] args) {

5

// Create a Scanner

6

Scanner input = new Scanner(Organisation.in);

7

8

// Receive the amount

9

System.out.impress(

x

"Enter an corporeality in double, for example 11.56: ");

enter input

xi

double amount = input.nextDouble();

12

xiii

int remainingAmount = (int)(amount * 100);

14

fifteen

// Find the number of one dollars

dollars

16

int numberOfOneDollars = remainingAmount / 100;

17

remainingAmount = remainingAmount % 100;

xviii

nineteen

// Find the number of quarters in the remaining amount

quarters

20

int numberOfQuarters = remainingAmount / 25;

21

remainingAmount = remainingAmount % 25;

22

23

// Detect the number of dimes in the remaining amount

dimes

24

int numberOfDimes = remainingAmount / 10;

25

remainingAmount = remainingAmount % 10;

26

27

// Find the number of nickels in the remaining amount

nickels

28

int numberOfNickels = remainingAmount / 5;

29

remainingAmount = remainingAmount % 5;

xxx

31

// Find the number of pennies in the remaining amount

pennies

32

int numberOfPennies = remainingAmount;

33

34

// Brandish results

set output

35

Arrangement.out.println("Your amount " + amount + " consists of \n" +

2.fourteen Problem: Counting Budgetary Units 49

36 "\t" + numberOfOneDollars + " dollars\n" +

37 "\t" + numberOfQuarters + " quarters\northward" +

38 "\t" + numberOfDimes + " dimes\n" +

39 "\t" + numberOfNickels + " nickels\n" +

40 "\t" + numberOfPennies + " pennies");

41 }

42 }

Enter an amount in double, for example 11.56: 11.56 Your amount xi.56 consists of

11 dollars

2 quarters

0 dimes

i nickels

1 pennies

line#

eleven

13

16

17

20

21

24

25

28

29

32

variables

Amount

11.56

remainingAmount

1156

56

six

6

1

numberOfOneDollars

11

numberOfQuarters

2

numberOfDimes

0

numberOfNickles

1

numberOfPennies

1

The variable amount stores the amount entered from the console (line 11). This variable is non inverse, because the corporeality has to be used at the end of the program to display the results. The program introduces the variable remainingAmount (line thirteen) to store the changing remainingAmount.

The variable corporeality is a double decimal representing dollars and cents. Information technology is converted to an int variable remainingAmount, which represents all the cents. For instance, if amount is 11.56, then the initial remainingAmount is 1156. The division operator yields the integer part of the division. So 1156 / 100 is xi. The residual operator obtains the remainder of the division. So 1156 % 100 is 56.

The plan extracts the maximum number of singles from the total amount and obtains the remaining corporeality in the variable remainingAmount (lines 16–17). It then extracts the maximum number of quarters from remainingAmount and obtains a new remainingAmount (lines twenty–21). Standing the same process, the plan finds the maximum number of dimes, nickels, and pennies in the remaining corporeality.

I serious trouble with this example is the possible loss of precision when casting a loss of precision double amount to an int remainingAmount. This could lead to an inaccurate consequence. If you

endeavour to enter the corporeality 10.03, 10.03 * 100 becomes 1002.9999999999999. You will notice that the program displays 10 dollars and two pennies. To prepare the problem, enter the amount as an integer value representing cents (run across Exercise 2.nine).

fifty Chapter ii Elementary Programming

Equally shown in the sample run, 0 dimes, i nickels, and 1 pennies are displayed in the issue. It would be better not to display 0 dimes, and to brandish 1 nickel and 1 penny using the singular forms of the words. You volition learn how to use selection statements to modify this program in the adjacent chapter (see Do iii.7).

concatenating strings and numbers

reading strings

two.fifteen The Cord Type

The char type represents just 1 character. To represent a string of characters, use the information type chosen String. For example, the post-obit lawmaking declares the message to exist a cord with value "Welcome to Coffee".

String bulletin = "Welcome to Java";

String is actually a predefined class in the Java library just similar the classes System, JOptionPane, and Scanner. The String type is not a primitive type. It is known as a reference type. Whatever Java class can be used as a reference blazon for a variable. Reference data types will be thoroughly discussed in Chapter 8, "Objects and Classes." For the time being, you need to know only how to declare a String variable, how to assign a string to the variable, and how to concatenate strings.

Equally commencement shown in Listing two.1, ii strings can be concatenated. The plus sign (+) is the concatenation operator if one of the operands is a string. If 1 of the operands is a nonstring (e.g., a number), the nonstring value is converted into a string and concatenated with the other string. Here are some examples:

// Three strings are concatenated

String message = "Welcome " + "to " + "Java";

// String Chapter is concatenated with number 2 String due south = "Chapter" + 2 ; // s becomes Chapter2

// Cord Supplement is concatenated with character B String s1 = "Supplement" + 'B' ; // s1 becomes SupplementB

If neither of the operands is a string, the plus sign (+) is the addition operator that adds two numbers.

The autograph += operator tin also exist used for string chain. For case, the following lawmaking appends the string "and Java is fun" with the string "Welcome to Java" in bulletin.

message += " and Java is fun";

So the new message is "Welcome to Coffee and Java is fun".

Suppose that i = 1 and j = 2, what is the output of the post-obit statement?

System.out.println("i + j is " + i + j);

The output is "i + j is 12" because "i + j is " is concatenated with the value of i offset. To force i + j to be executed starting time, enclose i + j in the parentheses, as follows:

System.out.println("i + j is " + ( i + j) );

To read a string from the console, invoke the next() method on a Scanner object. For case, the following code reads iii strings from the keyboard:

Scanner input = new Scanner(System.in); System.out.println("Enter three strings: "); String s1 = input.adjacent();

hazeloffew1978.blogspot.com

Source: https://studfile.net/preview/6231412/page:10/

0 Response to "Java Program to Read Monetary Amount in Cents"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel