You are on page 1of 10

Exercise 1

1. Create a new Java file named exercise1 and add it to the lab1 project.
2. Copy the following program into the file exercise1.
1. Compile the above program (Build Compile ile! and correct the errors if any generated.
2. "f you obtain #Process completed$ in the Build window% then run your program to obtain the
following output&
class irst'rogram
(
public static void main()tring*+ args!
(
int answer,
)ystem.out.println(-.ello reader.-!,
)ystem.out.println(-/elcome to Java.-!,
)ystem.out.println(-0et1s demonstrate a simple calculation.-!,
answer 2 2 3 2,
)ystem.out.println(-2 plus 2 is - 3 answer!,
4
4
1
2
Exercise 2
1. Create a new Java file named exercise2 and add it to the lab1 project.
2. Complete the following code to answer the next 5uestions.
a. 6eclare three variables int x% double y% and float 7.
b. "nitiali7e the three variables above (x 2 8% y 2 2.9% and 7 2 :.2!.
c. 6isplay the result of each of the following arithmetic expressions using a )ystem.out.println ( !
statement. "n the Interpretation column% find out the precedence rules discussed in the lecture.
Arithmetic Expression Result Interpretation
x 3 y ; 7
x < y ; 7
x < 2 3 y < 2
x < 2
x = 2
x = : ; 9 3 1
(y 3 9! ; 2
7 < (1 3 1!
9. 6eclare an int variable i initiali7e it to >% and then test the following increment and decrement
statements. Comment on the obtained output.
? University of Hail Department of Computer
Science and Software Engineering Statement
Output Comments
)ystem.out.println (33 i!,
)ystem.out.println (i!,
)ystem.out.println (i33!,
)ystem.out.println (i!,
)ystem.out.println (@@i!,
)ystem.out.println (i@@!,
)ystem.out.println (i!,
9
<<Ahis program calculates the userBs gross pay
import java.util.)canner, <<to be able to read from the Ceyboard
public class 'ay
(
public static void main()tring *+ args!
(
<<create a )canner object to read from the Ceyboard
)canner Ceyboard 2 new )canner()ystem.in!,
<<identifier declarations
double hours, <<number of hours worCed
double rate, <<hourly pay rate
double pay, <<gross pay
<<display prompts and get input
)ystem.out.print(-.ow many hours did you worCD -!,
hours 2 Ceyboard.next6ouble(!,
)ystem.out.print(-.ow much do you get paid per hourD -!,
rate 2 Ceyboard.next6ouble(!,
<<calculations
if(hours E2 >F!
pay 2 hours ; rate,
else
pay 2 (hours @ >F! ; (1.: ; rate! 3 >F ; rate,
<<display results
)ystem.out.println(-Gou earned H- 3 pay!,
4
4
>
<<Ahis program calculates the total price which includes sales tax
import java.util.)canner,
public class )alesAax
(
public static void main()tring*+ args!
(
<<identifier declarations
final double AIJKLIAM 2 F.F::,
double price,
double tax
double total,
)tring item,
<<create a )canner object to read from the Ceyboard
)canner Ceyboard 2 new )canner()ystem.in!,
<<display prompts and get input
)ystem.out.print(-"tem description& -!,
item 2 Ceyboard.next0ine(!,
)ystem.out.print(-"tem price& H-!,
price 2 Ceyboard.next6ouble(!,
<<calculations
tax 2 price 3 AIJKLIAM,
total 2 price ; tax,
<<display results
)ystem.out.print(item 3 - H-!,
)ystem.out.println(price!,
)ystem.out.print(-Aax H-!,
)ystem.out.println(tax!,
)ystem.out.print(-Aotal H-!,
)ystem.out.println(total!,
4
4
:
Ahis is a geometry calculator
Choose what you would liCe to calculate
1. ind the area of a circle
2. ind the area of a rectangle
9. ind the area of a triangle
>. ind the circumference of a circle
:. ind the perimeter of a rectangle
N. ind the perimeter of a triangle
Mnter the number of your choice&
N
import java.util.)canner,
<;;
Ahis program demonstrates static methods
;<
public class Oeometry
(
public static void main ()tring *+ args!
(
int choice, <<the userBs choice
double value 2 F, <<the value returned from the method
char letter, <<the G or P from the userBs decision
<<to exit
double radius, <<the radius of the circle
double length, <<the length of the rectangle
double width, <<the width of the rectangle
double height, <<the height of the triangle
double base, <<the base of the triangle
double side1, <<the first side of the triangle
double side2, <<the second side of the triangle
double side9, <<the third side of the triangle
<<create a scanner object to read from the Ceyboard
)canner Ceyboard 2 new )canner ()ystem.in!,
<<do loop was chose to allow the menu to be displayed
<<first
do
(
<<call the printQenu method
choice 2 Ceyboard.next"nt(!,
switch (choice!
(
case 1&
)ystem.out.print(-Mnter the radius of the circle& -!,
radius 2 Ceyboard.next6ouble(!,
<<call the circleIrea method and
<<store the result
<<in the value
)ystem.out.println(-Ahe area of the circle is - 3 value!,
breaC,
case 2&
)ystem.out.print(-Mnter the length of the rectangle& -!,
length 2 Ceyboard.next6ouble(!,
)ystem.out.print(-Mnter the width of the rectangle& -!,
R
width 2 Ceyboard.next6ouble(!,
<<call the rectangleIrea method and store
<<the result in the value
)ystem.out.println(-Ahe area of the rectangle is - 3 value!,
breaC,
case 9&
)ystem.out.print(-Mnter the height of the triangle& -!,
height 2 Ceyboard.next6ouble(!,
)ystem.out.print(
-Mnter the base of the triangle& -!,
base 2 Ceyboard.next6ouble(!,
<<call the triangleIrea method and store
<<the result in the value
)ystem.out.println(-Ahe area of the triangle is - 3 value!,
breaC,
case >&
)ystem.out.print(-Mnter the radius of the circle& -!,
radius 2 Ceyboard.next6ouble(!,
<<call the circumference method and
<<store the result in the value
)ystem.out.println(
-Ahe circumference of the circle is - 3 value!,
breaC,
case :&
)ystem.out.print(
-Mnter the length of the rectangle& -!,
length 2 Ceyboard.next6ouble(!,
)ystem.out.print(-Mnter the width of the rectangle& -!,
width 2 Ceyboard.next6ouble(!,
<<call the perimeter method and store the result
<<in the value
)ystem.out.println(-Ahe perimeter of the rectangle is - 3 value!,
breaC,
case N&
)ystem.out.print(-Mnter the length of side 1 - 3-of the triangle& -!,
side1 2 Ceyboard.next6ouble(!,
)ystem.out.print(-Mnter the length of side 2 - 3-of the triangle& -!,
side2 2 Ceyboard.next6ouble(!,
)ystem.out.print(-Mnter the length of side 9 - 3-of the triangle& -!,
side9 2 Ceyboard.next6ouble(!,
<<call the perimeter method and store the result
<<in the value
)ystem.out.println(-Ahe perimeter of the - 3-triangle is - 3 value!,
breaC,
default&
?
)ystem.out.println(
-Gou did not enter a valid choice.-!,
4
<<consumes the new line character after the number
Ceyboard.next0ine(!,
)ystem.out.println(-6o you want to exit the program - 3-(G<P!D& -!,
)tring answer 2 Ceyboard.next0ine(!,
letter 2 answer.charIt(F!,
4while (letter S2 TGB UU letter S2 TyB!,
4
4
import java.util.Scanner;
public class triangle{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
double base;
double height;
System.out.println("Input base ");
base = keyboard.nextDouble();
System.out.println("Input height ");
height = keyboard.nextDouble();
double c= area(base, height);
System.out.println("The area is "+c);
}
public static double area(double num1, double num2){
double c = 0.5*num1*num2;
return c;
}
}
8
import java.util.Scanner;
public class temp{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
double celsius;
System.out.println("Input Celsius ");
celsius = keyboard.nextDouble();
double c= fahrenheit(celsius);
System.out.println("The equivalent Fahrenheit is "+c);
}
public static double fahrenheit(double num1){
double c = ((num1*9)/5) + 32;
return c;
}
}
1F

You might also like