Chapter 2 Elementary Programming Program Listings
Contents Listing 2.1 Compute Area... 3 Listing 2.2 Compute Area With Console Input... 4 Listing 2.3 Compute Average... 5 Listing 2.4 Compute Area With Constant... 6 Listing 2.5 Display Time... 7 Listing 2.6 Fahrenheit To Celcius... 8 Listing 2.7 Show Current Time... 9 Listing 2.8 Sales Tax... 10 Listing 2.9 Compute Loan... 11 Listing 2.10 Compute Change... 12
Listing 2.1 Compute Area public class ComputeArea // Variable declarations double radius; double area; radius = 20.0; area = radius * radius * 3.14159; System.out.println("The area for the circle of radius " + radius + " is " + area);
Listing 2.2 Compute Area With Console Input public class ComputeAreaWithConsoleInput // Prompt the user to enter the radius System.out.println("Enter the radius: "); double radius = input.nextdouble(); // Calculate the area double area = radius * radius * 3.14159; // Display results System.out.println("The area for the cirlce of radius " + radius + " is " + area);
Listing 2.3 Compute Average public class ComputeAverage // Prompt the user to enter three numbers System.out.println("Enter three numbers: "); double number1 = input.nextdouble(); double number2 = input.nextdouble(); double number3 = input.nextdouble(); // Compute average double average = (number1 + number2 + number3) / 3.0; // Display results System.out.println("The average of " + number1 + " " + number2 + " " + number3 + " is " + average);
Listing 2.4 Compute Area With Constant public class ComputeAreaWithConstant final double PI = 3.14159; // Prompt the user to enter the radius System.out.println("Enter the radius: "); double radius = input.nextdouble(); // Calculate the area double area = radius * radius * PI; // Display results System.out.println("The area for the cirlce of radius " + radius + " is " + area);
Listing 2.5 Display Time public class DisplayTime // Prompt the user for input System.out.println("Enter an integer for seconds: "); int seconds = input.nextint(); int minutes = seconds / 60; int remainingseconds = seconds % 60; // Display results System.out.println(seconds + " seconds is " + minutes + " minutes and " + remainingseconds + " seconds");
Listing 2.6 Fahrenheit To Celcius public class FahrenheitToCelsius // Prompt the user for input System.out.println("Enter a degree in Fahrenheit: "); double fahrenheit = input.nextdouble(); double celsius = (5.0 / 9.0) * (fahrenheit - 32.0); // Display results System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius");
Listing 2.7 Show Current Time public class ShowCurrentTime // Obtain time in milliseconds since Jam1, 1970 long totalmilliseconds = System.currentTimeMillis(); // Convert the total milliseconds into seconds long totalseconds = totalmilliseconds / 1000; // Obtain Seconds long currentsecond = totalseconds % 60; // Obtain Minutes long totalminutes = totalseconds / 60; long currentminute = totalminutes % 60; // Obtain Hours long totalhours = totalminutes / 60; long currenthour = totalhours % 24; // Display time System.out.println("Current time is " + currenthour + ":" + currentminute + ":" + currentsecond + " GMT");
Listing 2.8 Sales Tax public class SalesTax // Prompt the user for input System.out.println("Enter purchase amount: "); double purchaseamount = input.nextdouble(); double tax = purchaseamount * 0.06; // Display results System.out.println("Sales tax is $ " + (int) (tax * 100) / 100.0);
Listing 2.9 Compute Loan public class ComputeLoan // Prompt user for annual interest rate System.out.println("Enter annual interest rate, e.g., 7.25 %: "); double annualinterestrate = input.nextdouble(); double monthlyinterestrate = annualinterestrate / 1200; // Prompt user for length of the loan System.out.println("Enter number of years as an integer, e.g., 5: "); int numberofyears = input.nextint(); // Prompt user for the amount System.out.println("Enter loan amount, e.g., 120000.95: "); double loanamount = input.nextdouble(); // Calculate payment double monthlypayment = loanamount * monthlyinterestrate / (1-1 / Math.pow(1 + monthlyinterestrate, numberofyears * 12)); double totalpayment = monthlypayment * numberofyears * 12; //Display results System.out.println("The monthly payment is $ " + (int) (monthlypayment * 100) / 100.0); System.out.println("The total payment is $ " + (int) (totalpayment * 100) / 100.0);
Listing 2.10 Compute Change public class ComputeChange // Prompt user for the amount System.out.println("Enter an amount in double, e.g., 11.56: "); double amount = input.nextdouble(); int remainingamount = (int) (amount * 100); // Calculate dollars int numberofonedollars = remainingamount / 100; remainingamount %= 100; // Calculate Quarters int numberofquarters = remainingamount / 25; remainingamount %= 25; // Calculate Dimes int numberofdimes = remainingamount / 10; remainingamount %= 10; // Calculate Nickels int numberofnickels = remainingamount / 5; remainingamount %= 5; // Calculate Pennies int numberofpennies = remainingamount; // Display Results System.out.println("Your amount " + amount + " consists of "); System.out.println(" " + numberofonedollars + " dollars."); System.out.println(" " + numberofquarters + " quarters."); System.out.println(" " + numberofdimes + " dimes.");
System.out.println(" " + numberofnickels + " nickels."); System.out.println(" " + numberofpennies + " pennies.");