[go: up one dir, main page]

0% found this document useful (0 votes)
9 views2 pages

Java Homework 1_f36a0eb81b5fc74d48f61311bd4aba49

Uploaded by

hrnhg2c6r7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java Homework 1_f36a0eb81b5fc74d48f61311bd4aba49

Uploaded by

hrnhg2c6r7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question #1:

A- Which of the following are valid java identifiers?


 _myProgram.
 MIX-UP
 C++Program2
 quiz@ 7
 Prg$$Lec
 1footEQ12INCH
 The answer is : _myProgram/ Prg$$Lec

B- Evaluate the following expressions:


 20 - 12 / 4 * 2
14
 5 % 7+32 / 4
13
28 - 5 / 2.0
25.5
 17 + 5 % 2 – 3
15
 15.0 + 3.0 * 2.0 / 5.0
26.2
Question #2:
Write Java statements that accomplish the following:
a. Declare int variables x and y.
b. Initialize an int variable x to 10 and a char variable ch to 'B'.
c. Update the value of an int variable x by adding 5 to it.
d. Declare and initialize a double variable payRate to 12.50.
e. Copy the value of an int variable firstNum into an int variable tempNum.
f. Swap the contents of the int variables x and y. (Declare additional variables, if necessary.
int x, y‫؛‬
x=10;
char ch='B' ;
x=x+5;
double payRate=12.50;
int firstNum= 13;
int tempNum= firstNum;
x=x*4;
y=7;

Programming Section:
Question #3:
Write a Java program that prompts the user to input height of three trees, then find and print the average
for these heights.

public static void main (String[] args) {


Scanner in=new Scanner (System. in) :
System. out println ("enter number") :
double hl= in.nextDouble ():
System. out println ("enter number") ;
double h2=in.nextDouble () ;
System. out println ("enter number") ;
double h3=in.nextDouble () ;
double ave= (hl+h2+h3) /3;
System. out printin ("ave="+ave) :

Question #4:
Write a Java program that prompts the user to input the elapsed time for an event in hours, minutes, and seconds.
The program then outputs the elapsed time in seconds.
For example :
Enter # of hours=2
Enter # of minutes=35
Enter # of seconds=45
Elapsed Time is 9345

public static void main (Stringll args) {


Scanner in-new Scanner (System. in) ;
System. out println ("enter hour, minutes, seconds") ; double hour, minutes, seconds;
hour=in.nextDouble() :
minutes=in.nextDouble ():
seconds=in. nextDouble () :
double a= (hour*60) *60+minutes*60+seconds;
System. out. println(a):

Question #5:
Write a Java program that prompts the user to input temperature in Fahrenheit, then find and print temperature
inCelsius.
(hint used the formula : C= (F-32*5)/9

public static void main (String[l args)


Scanner in =new Scanner (System. in) :
System. out printin ("enter number") ;
double F= in.nextDouble();

double C= (F-32*5) / 9;
System. out.printin ("C="+c);

You might also like