Survey

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
```ICS102: Computer Programming
Department of Computer Science & Software Engineering,
University of Hail, Kingdom of Saudi Arabia
Lab 5 Solution
Prepared By:Noor ALzaareer
Lab 5 objectives
The aims of this lab are:
- Learn to use Scanner class
- Use Scanner class to input numbers values and String values
Exercise 1
1. Create an empty project called Exercise1 and save it in the folder
Z:\ICS102\Labs\.Lab5\Exercise1 created earlier.
2. Create a new Java file named Ex1 and add it to the project.
3. Complete the program below to allow you enter your name ,and your Id using
Scanner class and then print what you entered using println method. To obtain the
following Sample output
Huda Alenzi
12
//Import Scanner class
import java.util.Scanner;
class Ex1
{ public static void main(String[] args) {
String name;
double age;
//create object keyboard from Scanner class
Scanner keyboard=new Scanner(System.in);
//write prompt input for name
// use keyboard to input the value of name
name=keyboard.nextLine();
//write prompt input for age
// use keyboard to input the value of age
age=keyboard.nextDouble();
//print the value of name what is entered
}}
Exercise 2
1. Create an empty project called Exercise2 and save it in the folder
Z:\ICS102\Labs\.Lab5\Exercise2 created earlier.
2. Create a new Java file named Ex2 and add it to the project.
3. Complete the program below to compute the area for Rectangle based on the
following formula: Area=length*width
Your program should allow you to enter the value for length and width of the
rectangle.
Enter the side of Square
9
The Area of Square is 81.0
//Import Scanner class
import java.util.Scanner;
class Ex2
{public static void main(String [] args)
{// create object key from Scanner class
Scanner key=new Scanner(System.in);
//define variable to store the value of side
double side;
//define variable to store the value of Square’s area
double area;
//write prompt input for side
System.out.println ("Enter the side of Square");
// use object key to input the Side value
side=key.nextDouble();
// compute the area as the above formula area=side*side
area=side*side
// print the value of area using prinln() method
System.out.println("the Area of Square is "+area);}}
```