Java laboratory Activity by Dwight Sabio

download Java laboratory Activity by Dwight Sabio

of 10

Transcript of Java laboratory Activity by Dwight Sabio

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    1/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 thefollowing 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(-0ets demonstrate a simple calculation.-!,

    answer 2 3 2,

    )ystem.out.println(-2 plus 2 is - 3 answer!,

    4

    4

    1

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    2/10

    2

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    3/10

    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 8% y 2.9% and 7 :.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 decrementstatements. Comment on the obtained output.

    ? University of Hail Department of ComputerScience 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

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    4/10

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    5/10

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    6/10

    Ahis is a geometry calculator

    Choose what you would lie to calculate1. ind the area of a circle2. ind the area of a rectangle9. ind the area of a triangle>. ind the circumference of a circle:. ind the perimeter of a rectangleN. ind the perimeter of a triangleMnter the number of your choice&

    N

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    7/10

    import java.util.)canner,

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    8/10

    width eyboard.next6ouble(!,

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    9/10

    )ystem.out.println(-Gou did not enter a valid choice.-!,4

  • 8/12/2019 Java laboratory Activity by Dwight Sabio

    10/10

    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