Question: Lab 2.1: Pg.: 127 Exercise 1, 2, 3 and 7. Pg.: 130 Practice Programs: 1,2 ,and 3 Pg.: 131 Programming Projects 1,2,3,5,and 8 1-write a
Lab 2.1:
Pg.: 127
Exercise 1, 2, 3 and 7.
Pg.: 130
Practice Programs: 1,2 ,and 3
Pg.: 131
Programming Projects 1,2,3,5,and 8
1-write a program that demonstrate the approximate nature of floating-point values by performing the following tasks:
Use the Scanner to read a floating-point value x
Compute 1.0/x and store the result in y.
Display x,y, and the product of x and y.
Subtract 1 from the product of x and y and display the result
Try your program with values of x that range from 2e-11 to 2e11. What can you conclude?
2-Write a program that demonstrate type casting of double values by performing the following tasks:
Use the Scanner to read a floating-point value x
Type cast x to an int value and store the result in y.
Display x,y clearly labeled.
Type cast x to a byte value and store the result in z.
Display x and z clearly labeled.
Try your program with positive and negative values of x that range in magnitude 2e-11 to 2e11. What can you conclude?
3- Write a program that demonstrate the operator % by performing the following tasks:
Use the Scanner to read a floating-point value x
Compute x % 2.0 and store the result in y.
Display x and y clearly labeled.
Type cast x to an int value and store the result in z.
Display x,z and z % 2 clearly labeled.
Try your program with positive and negative values of x. What implications do your results have for deciding whether a negative integer is odd?
7-Write some Java statements that use the String methods indexof and sub-string to find the first word in a string. We define word to be a string of characters that does not include whitespaces. For example, the first word of the string
Hello, my good friend!
Is the string Hello, and the second word is the string my.
Practice P(s):
1-Write a program that reads three whole numbers and display the average of the three numbers.
2-Write a program that uses Scanner to read two strings from the keyboard. Display each string, along with its length, on two separate lines, Then create a new string by joining the two strings, separated by a blank. Display the new string and its length on a third line.
3-Write a program that reads the amount of a monthly mortgage payment and the amount still owed- the outstanding balance and then display the amount of the payment that goes to interest and the amount that goes to principal(i.e., the amount that goes to reducing the debt). Assume that the annual interest rate is 7.49 percent. Use a named constant for the interest rate.
Note that payments are made monthly, so the interest is only one twelfth of the annual interest of 7.49 percent.
Programming P(s):
1-Write a program that converts degrees from Fahrenheit to Celsius, using the formula
DegreeC = 5(DegreeF 32)/9
Prompt the user to enter a temperature in degree Fahrenheit as a whole number without a fractional part. Then have the program display the equivalent Celsius temperature, including the fractional part to at least one decimal point. A possible dialogue with the user might be:
Enter a temperature in degrees Fahrenheit: 72
72 degrees Fahrenheit is 22.2 degrees Celsius.
2-Write a program that reads a line of text and then displays the line, but with the first occurrence of hate changed to love. For example, a possible sample dialogue might be:
Enter a line of text.
I hate you.
I have rephrased that line to read:
I love you.
You can assume that the word hate occurs in the input. If the word hate occurs more than once in the line, your program will replace only its first occurrence.
3-Write a program that will read a line of text as input and then display the line with the first word moved to the end of the line. For example, a possible sample interaction with the user might be:
Enter a line of text. No punctuation please.
Java is the language
I have rephrased that line to read:
Is the language Java
Assume that there is no space before the first word and the end of the first word is indicated by a blank, not by a comma or other punctuation.
Note that the new first word must begin with a capital letter.
5-Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cents increments(25,30,35,,90,95,100),and the machine accepts only a single dollar bill to pay for the item. For example, a possible dialogue with the user might be:
Enter price of item
(from 25 cents to a dollar, in 5-cent increments):45
You bought an item for 45 cents and gave me a dollar,
So your change is
2 quarters,
0 dimes, and
1 nickel.
8-The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR.
The calories needed for a woman to maintain her weight is:
BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) (4.7 * age in years)
The calories needed for a man to maintain this weight is:
BMR = 66 + (6.3 * weight in pounds) + (12.9 * height in inches) (6.8 * age in years)
A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input his or her weight in pounds, height in inches, and age in years. The program should then output the number of chocolate bars that should be consumed to maintained ones weight for both a woman and a man of the input weight, height, and age.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
