> Java Questions?

Java Questions?

Posted at: 2014-12-18 
Don't ask or expect others to write your code for you. Do as much as you can initially and then ask for help with things that don't work or you don't understand. Start with pseudo code.

If I remember correctly, the math.random() function generates a number between 0 and 1. However, it is supposed to be (double)math.random. Making it an int will turn it in to a 0 (the conversion truncates, not rounds). So that would generate such a 0, multiply it by 6, and then add one to it. The answer would always be 1.

For number two, take the math.random output and do whatever you think you would have to do to generate an integer between 1 and 100 ... it shouldn't be that tough to figure out. If you can't, drop the class.

1. pseudo code:

// create variables to store user input, pst, gst and total

// ask the user for input

// calculate the GST

// calculate the PST (or reverse those two ... I don't know what PST or GST are.

// calculate the total

// output the cost, GST, PST, and total.

2. // create variable for height input and metric result.

// create a constant which has a value of 2.54

//ask user for input

// set the metric result variable to the user input times the constant

// output "xxx inches equals xxx cm.

For number 3, use the exact same program, change the constant and the text as appropriate.

A student incorrectly attempted to produce a random value uniformly distributed over the set (1,2,…,6) using the expression

6*(int)math.random()+1

State and explain the actual value of the expression.

2. Write a Java program to generate a random integer between 1 and 100.

Constant

Make sure to use constant in the following questions.



1. Cost.java Write a program that asks user for the price of an item, then output the amount of PST and GST for the item, followed by the total cost of the item.



2. Height.java There are 2.54 cm in one inch. Write a program to input the users height in inches and output their height in centimeters. Use a constant for the conversion factor. Be sure to prompt fro the input and to label the output.



3. Currency.java Write a program that asks for an amount of United States money and then prints the Canadian equivalent. Canadian = U.S * 1.8



ICS E-book p. 22 (Variables and Numbers.pdf)

4. Critical Thinking #2 (every other letter starting from a)