Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. How input stream works: consider the following program that reads the age and

ID: 3637730 • Letter: 1

Question

1. How input stream works: consider the following program that reads the age and name of person from the keyboard and display them.
import java.util.Scanner;
public class dataInput
{
public static void main (String[] args)
{
String name; // Name of the user
int age;

Scanner scan = new Scanner(System.in);

System.out.print ("Enter your age: ");
age = scan.nextInt();

System.out.print ("Enter your name:");
name = scan.nextLine();

System.out.println(name + " you are " + age + " years old");
}
}

Type, compile and run the program.
a) When it asks for input enter 45 and press enter key. Observe what happens. Don’t be panic! Go to step b
b) Now rerun the program, when it asks for data , type following input and press the enter key

45 Josh Williams

Observe the output.
c) Can you explain the reason for the program to skip name input in part a? How do you modify the program in part (a) so that you can enter data and run the program properly?

2. Given the declarations below, find the result of each expression.
int a = 3, b = 10, c = 7;
double w = 12.9, y = 3.2;

i. a + b * c
ii. a - b / c
iii. (a + w) / b
iv. a % b / y + w
v. b/a + w/

part 2
Ideal Weight Program:
Write a program to compute the ideal weight for both males and females. According to one study, the ideal weight for a female is 100 pounds plus 5 pounds for each inch in height over 5 feet. For example, the ideal weight for a female who is 5'3" would be 100 + 3*5 = 115 pounds. For a male the ideal weight is 106 pounds plus 6 pounds for each inch in height over 5 feet. For example, the ideal weight for a male who is 5'3" would be 106 + 3*6 = 124 pounds. Your program should ask the user to enter his/her height in feet and inches (both as integers—so a person 5'3" would enter the 5 and the 3). It should then compute and print both the ideal weight for a female and the ideal weight for a male. The general outline of your main function would be as follows:
? Declare your variables (think about what variables you need—you need to input two pieces of information (what?), then you need some variables for your calculations (see the following steps)
? Get the input (height in feet and inches) from the user
? Compute the total number of inches of height (convert feet and inches to total inches, note: 1 foot equal 12 inches and hence 5 feet equal 60 inches)
? Compute the ideal weight for a female and the ideal weight for a male ( please see the assignment for sample calculation )
? Print the answers

Plan your program, then type it in, compile and run it. Be sure it gives correct answers.
When a user runs your program, he/she should see the following output

Program output:
Welcome to Ideal Weight Calculation Program

Please enter your height, feet followed by inches (eg; 5 3): 5 3
If you are a female, your ideal weight should be 115 pounds.
If you are a male, your ideal weight should be 124 pounds.

Explanation / Answer

import employees.*; import util.*; public class Payroll { public static void main(String[] args) throws Exception { KeyboardReader kbr = new KeyboardReader(); String fName = null; String lName = null; int dept = 0; double payRate = 0.0; double hours = 0.0; int numDeps = 0; String dfName = null; String dlName = null; Employee e = null; fName = kbr.getPromptedString("Enter first name: "); lName = kbr.getPromptedString("Enter last name: "); dept = kbr.getPromptedInt("Enter department: "); do { payRate = kbr.getPromptedFloat("Enter pay rate: "); if (payRate < 0.0) System.out.println("Pay rate must be >= 0"); } while (payRate < 0.0); e = new Employee(fName, lName, dept, payRate); numDeps = kbr.getPromptedInt("How many dependents? "); for (int d = 0; d