Exercise I

advertisement
Exercise I
[1] Fill in the 10 blanks in the program below:
/*
The user enters three integers. The program prints the su
m, average, and product of these numbers.
*/
import _java.util.Scanner;
public class Numcomputation{
public static void main(String[] args)
{
int num1, num2, num3, sum, __product___________;
double __average_________;
System.out.println("Enter 3 integers: ");
Scanner scanner = new Scanner( System.in );
System.out.print( "The first integer: " );
String input = scanner.nextLine();
num1 = Integer.parseInt( input );
System.out.print( "The second integer: " );
String input2 = scanner.nextLine();
num2 = Integer.parseInt( input 2);
System.out.print( "The third integer: " );
String input3 = scanner.nextLine();
num3 = Integer.parseInt( input 3);
__sum___________ = num1 + num2+ num3;
System.out.println(num1 +"+" + ___num2________ + "+" +num3
+ "=" +sum);
product = num1 * num2 * num3;
System.out.println("Product is "+ __product___________ );
average = sum / 3.0;
System.out.println("Ave is " + ___average__________ );
}
}
[2] Using Java, let socialSecurityNum be a variable of type int.
(a) Show how to declare this variable and initialize it to the value 123456789
using Java.
Int socialSecurityNum;
SocialSecurityNum =123456789;
Or 1 step:
Int socialSecurityNum=123456789;
(b) In general, can we store any 9-digit social security number using the data
type int? Explain.
Yes, the maximum value that int can hold is 2,147,483,647.
Int is 32 bits
(c) What is the output of the following two statements, if
socialSecurityNum stores the value 123456789? Explain how the Java
computes each expression.
System.out.println(socialSecurityNum / 10000);
12345
System.out.println(socialSecurityNum % 1000)); 789
[3] Predict the output that would be shown in the terminal window when the fol
lowing program fragments are executed:
Fragment
output
Error Message!
int myint = 3.23;
System.out.println(myint);
2
int calc = 15 % 7 * 2;
System.out.println(calc);
System.out.println("Hi ");
System.out.println("WIT,\n COMP128n
is");
System.out.println("very fun! ");
Hi
WIT,
COMP128n is
Very fun!
1.0
double result;
result = 7 / 4;
System.out.println(result);
3.0
double result;
result = 5 / 2 * 1.5;
System.out.println(result);
[4] Translate these mathematical expressions to Java expressions:
a)
Read Java library of Math.
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html
In the Java docs, look for a list of methods in the method summary. Choose serveral
methods and write a brief description for each.
Example:
abs(double a):
Returns the absolute value of a double value.
b)
x=(5*Math.pow(x,3) –x + 7)/(2*Math.pow(x,2) );
x
5x3  x  7
2x2
x
 b  b 2  4ac
2a
c  sin x  cos x
5] Write a Java program, that reads in 3 double typed numbers a,b,c and prints the s
olution to the linear equation: ax+b=c . Name your program Question4.
[6] Write a program that converts a temperature from Fahrenheit to Celsius. Th
e user inputs a temperature in degrees Fahrenheit, and the program prints out
the temperature in degrees Celsius. Your code must follow the output in the sa
mple run below. Name your program Question5.
Recall that to convert from Fahrenheit to Celsius you use this formula:
/* sample run:
Enter the temperature in Fahrenheit: 80
That is 26.6667 degrees Celsius.
*/
Download