Download Lab6 Solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* 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 6
Lab 6 objectives
The aims of this lab are:
- Learn how to use if else statement and switch case statement.
Exercise 1
1. Create an empty project called Exercise1 and save it in the folder
Z:\ICS102\Labs\.Lab6\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 an integer number using
Scanner class and then check whether given number is odd or even
using if else statement.
The program should obtain the output as below.
Output:
Enter a number
23
23 is odd
//Import Scanner class
import java.util.Scanner;
class Ex1{
public static void main(String[]args){
int n;
Scanner key=new Scanner (System.in);
System.out.println(“Enter Integer number”);
n=key.nextInt( );
if(n%2==0)
System.out.println(n+”is even”);
else
System.out.println(n+”is odd”); }}
Exercise2
1. Create
an
empty project
called
Exercise2
and
save
it
in
the
folder
Z:\ICS102\Labs\.Lab6\Exercise2 created earlier.
2. Create a new Java file named Ex2 and add it to the project.
3. Write a java program program that assigns a price to each item. Use the table below
Item
Price
Coffee
3 SR
Tea
4 SR
Cappuccino
5 SR
Mocha
7 SR
Hot Chocolate
8 SR
Latte
9 SR
Hints:
1- Displays the following output on the screen:
************************
*
AJA CAFE
*
************************
* 1.Coffee
*
* 2.Tea
*
* 3.Cappuccino
*
* 4.Moka
*
* 5.Hot Chocolate
*
* 6.Latte
*
***********************
Please Entre Your Choice:
2- Use Scanner class to read from the screen.
3- Use Switch statement to display the price of the item.
Solution:
import java.util.Scanner;
public class EX2{
public static void main(String[]args){
Scanner k=new Scanner(System.in);
System.out.println("************************");
System.out.println("*
AJA CAFE
*");
System.out.println("************************");
System.out.println("* 1.Coffee
System.out.println("* 2.Tea
*");
*");
System.out.println("* 3.Cappuccino
*");
System.out.println("* 4.Mocha
*");
System.out.println("* 5.Hot Chocolate
System.out.println("* 6.Latte
*");
*");
System.out.println("************************");
System.out.println("Please Entre Your Choice:");
System.out.println(" ");
int n=k.nextInt();
int tp;
switch(n){
case 1:System.out.println(" You want cofee, cofee price is 3 SR ");
break;
case 2:System.out.println(" You want Tea, Tea price is 4 SR ");
break;
case 3:System.out.println(" You want Cappuccino, Cappuccino price is 5 SR ");
break;
case 4:System.out.println(" You want Mocha, Mocha price is 7 SR ");
break;
case 5:System.out.println(" You want Hot Chocolate, Hot Chocolate price is 8 SR ");
break;
case 6:System.out.println(" You want Latte, Latte price is 9 SR ");
break;
default: System.out.println(" Exit");}
}}
Exercise 3
4. Create
an
empty project
called
Exercise3
and
save
it
in
the
folder
Z:\ICS102\Labs\.Lab6\Exercise3 created earlier.
5. Create a new Java file named Ex3 and add it to the project.
6. Write a java program that determines a student’s grade
Hint 1: First you should enter your average score, then:
• If the average score is 90% or more the grade is ‘A’ and she is excellent.
• If the average score is 70% or more and less than 90%, the grade is ‘B’ and
she is very good.
• If the average score is 60% or more and less than 70%, the grade is ‘C’ and
she is good.
• If the average score is 50% or more and less than 60%, the grade is ‘D’.
• If the average score is less than 50%, the grade is ‘F’ and she is not good.
Hint2:

Use both if then else and switch case statement
 If–else to obtain grade A, B, C….. as character type
 Switch Case to print an appropriate message which describes the grade
and student level.
Example: “Grade is A she is statement
.
Solution:
//Import Scanner class
import java.util.Scanner;
class Ex2{
public static void main(String[] args){
Scanner key=Scanner (System.in);
System.out.println("enter your average score");
double avg=input.nextDouble();
char grade;
if (avg>=90)
grade='A';
else if(avg>=70)
grade='B';
else if (avg>=60)
grade='C';
else if(avg>=50)
grade='D'
else
grade='F';
switch(grade){
case 'A': System.out.println("grade is A and She is Excellent");
break;
case 'B': System.out.println("grade is B and She is Verygood");
break;
case 'C': System.out.println("grade is C and She is good" );
break;
case 'D' : System.out.println("grade is D " );
break;
case 'F': System.out.println("grade is F she is not good" );
break;
}}
}