INPUT & OUTPUT I/O Example Using keyboard input for characters import java.util.scanner; class Echo{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard String indata; // String variable } } System.out.println("Enter the data:"); indata = sc.nextline(); // read a line from scanner System.out.println("You entered: " + indata ); I/O Example Using keyboard input for Integers import java.util.scanner; class EchoSquare{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard int num, square; // declare two int variables System.out.println("Enter an integer:"); num = sc.nextint(); // read an int from scanner square = num * num ; // compute the square } } System.out.println("The square of " + num + " is " + square);
Exercise 11 Area of a Circle Write a program that calculates the area of a circle from its radius. The radius will be an integer read in from the keyboard. The user dialog will look like this: D:\users\default>java CircleArea Input the radius: 3 The radius is: 3 The area is: 28.274333882308138 You will need to use the constant PI which you get by using Math.PI
Exercise 12 Cents to Dollars Write a program that reads in a number of cents. The program will write out the number of dollars and cents, like this: D:\users\default>java Dollars Input the cents: 324 That is 3 dollars and 24 cents. (For this program you will use integer arithmetic and will need to avoid floating point arithmetic. Review the integer remainder operator % if you are unsure how to proceed.)
Exercise 13 Correct Change When cashiers in a store give you change they try first to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents: One dollar fits into 163, leaving 63 cents. Two quarters fit into 63 cents, leaving 13 cents. One dime fits into 13 cents, leaving 3 cents. No nickels are needed. Three cents are left. Your change is : 1 dollar, two quarters, one dime, and three cents. Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. All variables and all math in this program will be integers. If you are stuck, it will help to do an example problem with paper and pencil.
Exercise 14 Ohm's Law Ohm's law relates the resistance of a electrical device (like a heater) to the electric current flowing through the device and the voltage applied to it. The law is: I = V/R Here, V is the voltage (measured in volts), I is the current (measured in amps), and R is the resistance (measured in ohms.) Write a program that asks the user for the voltage and the resistance of a device. The program will then write out the current flowing through it. Use floating point math. Since V and R are integers (converted from user input) you must use a trick to do floating point division. Change the equation to this: I = (V + 0.0)/R The math inside parentheses is done first. So V + 0.0 is done first, and since 0.0 is floating point, so will be the result.
FLOATING POINT INPUT Exercise 15 Power Costing Write a program that calculates the annual cost of running an appliance. The program will ask the user for the cost per kilowatt-hour and the number of kilowatt-hours the appliance uses in a year: Enter cost per kilowatt-hour in cents 8.42 Enter kilowatt-hours used per year 653 Annual cost: 54.9826
Exercise 16 Another Brick in the Wall When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The speed v is given by v = (1/2) g t 2 Here v is the speed in feet per second, t is the time in seconds, and g is 32.174. Write a program that asks the user for the number of seconds and then prints out the speed. Enter the number of seconds 5.4 Speed of the brick: 469.092 feet per second One hundred miles per hour is 146.67 feet per second. Use your program to determine approximately how long it takes the brick to reach that speed.
Exercise 17 Logarithms The base 2 logarithm of a number is defined by: log 2 X = n if 2 n = X For example log 2 32 = 5, because 2 5 = 32 log 2 1024 = 10, because 2 10 = 1024 Write a program that inputs a number and outputs its base 2 logarithm. Use floating point input. This problem would be easy, but the Math package does not have a base 2 logarithm method. Instead you have to do this: log 2 X = (log e X) / (log e 2) Here, log e X is the natural logarithm of X. Use this function in the Java Math package: Math.log( X ) When you use this, X must be a double. Write the program so that the user can enter floating point numbers. Enter a double: 998.65 Base 2 log of 998.65 is 9.963835330516641
Exercise 18 Harmonic Mean The harmonic mean of two numbers is given by: H = 2 / ( 1/X + 1/Y ) This is sometimes more useful than the more usual average of two numbers. Write a program that inputs two numbers (as floating point) and writes out both the usual average (the arithmetic mean) and the harmonic mean. Enter X: 12 Enter Y: 16 Arithmetic mean: 14.0 Harmonic mean: 13.714285714285715