Download Lab 6 Exercises

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

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

Document related concepts
no text concepts found
Transcript
Lab 6
Exercises
Exercise 1:
(even.java)
Write a program that reads an integer x and checks ether it is divisible by 2 , 3 and 5.
Print appropriate messages.
import java.util.Scanner;
class a {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number:");
int x = in.nextInt();
if (x%2==0 && x%3==0 && x%5==0)
System.out.println("divisible by 2,3 and 5");
else
System.out.println("invalid");
}}
Exercise 2:
(HonorCalculator.java)
Design and implement a Java program that takes a student GPA as an input and then prints a
message dialoge with the equavilant distinction (First, second or third distinction) as follows:
GPA >= 3.75
: First distinction
3.5 <= GPA < 3.75 : Second distinction
3.0 <= GPA < 3.5 : Third distinction
GPA < 3.0
: There is no distinction awarded
Hint: to read double value using Scanner object
import java.util.Scanner;
class a {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter your GPA:");
double x = in.nextDouble();
if (x>=3.75)
System.out.println("First Distinction");
else
if (x<3.75 && x>=3.5)
System.out.println("second Distinction");
else
if (x<3.5 && x>=3)
System.out.println("Third Distinction");
else
System.out.println("There is no distinction awarded"); }}
Exercise 3:
(Traffic.java)
Design and implement a Java program that get a traffic violation number and output the
traffic violation title and price based on the following table: Hint (use Switch)
Number
Title
Price
1
Not stopping at (STOP) signs
SR 100
2
Usage of improper light
SR 200
3
Sudden start-up (heeling)
SR 200
4
Violating speed limit
SR 100
5
Leaving the car unattended on public streets
SR 200 + Towing cost
6
Destroying the Denver boot
SR 2500 + Dismiss
else
Undefined
undefined
import java.util.Scanner;
public class a{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a traffic violation number");
int trafficVoilationNumber = keyboard.nextInt();
switch(trafficVoilationNumber){
case 1:
System.out.println("Title: Not stopping at (STOP) signs");
System.out.println("Price: SR 100");
break;
case 2:
System.out.println("Title: Usage of improper light");
System.out.println("Price: SR 200");
break;
case 3:
System.out.println("Title: Sudden start-up (heeling)");
System.out.println("Price: SR 200");
break;
case 4:
System.out.println("Title: Violating speed limit");
System.out.println("Price: SR 100");
break;
case 5:
System.out.println("Title: Leaving the car unattended on public streets");
System.out.println("Price: SR 200 + Towing cost");
break;
case 6:
System.out.println("Title: Destroying the Denver boot");
System.out.println("Price: SR 2500 + Dismiss");
break;
default :
System.out.println("Title: undefined");
System.out.println("Price: undefined");
break; }}}
Exercise 4:
(Menu.java)
Design and implement a Java program that prompts the user to enter 2 numbers of type int
then displays a menu on the screen as shown below:
a. Print
b. Print
c. Print
- Choose
sum
sum and average
sum, average, and max
an option [a, b, c]: _
The program reads the user’s option as a integer using Scanner object and then display the
result.
import java.util.Scanner;
public class Menu{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the first number (Integer)");
int number1 = keyboard.nextInt();
System.out.println("Enter the second number (Integer)");
int number2 = keyboard.nextInt();
System.out.println("a. Print sum ");
System.out.println("b. Print sum and average");
System.out.println("c. Print sum, average, and max");
System.out.println(" - Choose an option [a, b, c]:");
char choice = keyboard.next().charAt(0);
switch(choice){
case 'a':
System.out.println("Sum is : "+(number1+number2));
break;
case 'b':
System.out.println("Sum is : "+(number1+number2));
System.out.println("Average is :"+ ((number1+number2)/2));
break;
case 'c':
System.out.println("Sum is : "+(number1+number2));
System.out.println("Average is :"+ ((number1+number2)/2));
if(number1 >= number2)
System.out.println("max is :"+number1);
else
System.out.println("max is :"+number2);
break;
break;
default :
System.out.println("Sorry , invalid input");
break;
}}}
Lab 7
Exercise 1:
(UOH.java)
Design and implement a program that will print
(A) UOH IS THE BEST 10 times
(B) HAIL 5 times
NOTE 01: Use only 1 loop.
NOTE 02: Your output may be in any order but it is preferable to do as:
UOH IS
UOH IS
HAIL
UOH IS
UOH IS
HAIL
UOH IS
UOH IS
HAIL
...
THE BEST
THE BEST
THE BEST
THE BEST
THE BEST
THE BEST
class Lab7 {
public static void main(String[] args) {
for(int i=0;i<=4;i++){
System.out.println ("UOH IS THE BEST");
System.out.println ("UOH IS THE BEST");
System.out.println ("HAIL");
}
}
}
Exercise 2:
(Series.java)
i 150
Design and implement a program that will find the result of the following:
class Lab7 {
public static void main(String[] args) {
int total=0;
for(int i=53;i<=150;i++){
int sum=0;
sum=2*i+5;
total=total+sum;
}System.out.println (total);
}}
 (2i  5)
i  53
Exercise 3:
(Dividors.java)
Write a program that reads an alphabetic character and a number of repeat then prints a
triangle of alphabet. Example:
Enter an alphabetic character: t
Enter number of repeating: 5
The output is:
t
t
t
t
t
t
t t
t t t
t t t t
import java.util.Scanner;
class Lab {
public static void main(String[] args) {
System.out.println("Enter Char");
Scanner in=new Scanner(System.in);
String a=in.next();
System.out.println ("Enter number");
int n=in.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print (a);
}
System.out.println();
} }
}
Exercise 4:
(Swap.java)
Design and implement a program that will enclose exercises 1, 2 and 3 inside a do-while
loop, to make a user choose among the three which ones he wants to execute. The program
should show the following menu:
1. Exercise 01 - Print
2. Exercise 02 - Summation
3. Exercise 03 – Division
4. Exit
Enter your choice [1, 2, 3, or 4]:
import java.util.Scanner;
public class Lab7 {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int x= in.nextInt();
do{
switch(x){
case 1:
for(int i=0;i<=4;i++){
System.out.println ("UOH IS THE BEST");
System.out.println ("UOH IS THE BEST");
System.out.println ("HAIL");
}
break;
case 2:
int total=0;
for(int i=53;i<=150;i++){
int sum=0;
sum=2*i+5;
total=total+sum;
} System.out.println (total);
break;
case 3:
Scanner n3=new Scanner(System.in);
case 3:
Scanner n3=new Scanner(System.in);
int d=n3.nextInt();
System.out.println("Please enter a number:");
System.out.println("Numbers dividing"+ d +"are");
for(int i=1;i<d;i++){
if(d%i ==0){
System.out.println (i);
}}
break;
default:
in.close();
break;
}x++;
}while(x<=4);
}
}
Lab 8
Exercise #01:
(ArrayReversing.java)
Design and implement a java code that should reverse the sequence of elements in an array,
for example with an array containing
1 4 9 16 3 7
then the new array will be printed as:
7 3 16 9 4 1.
class Lab8ex1 {
public static void main(String[] args) {
int[] a= new int[6];
a[0]=1;
a[1]=4;
a[2]=9;
a[3]=16;
a[4]=3;
a[5]=7;
for(int i=0; i<a.length ; i++){
System.out.print(a[i]);
}
System.out.println();
for(int i=a.length-1; i >= 0; i--){
System.out.print(a[i]);
}
}
}
Exercise #02:
(ComaringArraysI.java)
Design and implement a java code that should check whether the two arrays a and b have
the same elements on some order, ignoring multiplicities.
For example, the two arrays
1 4 1
and
4 1
would be considered to have the same set.
class Lab8ex2 {
public static void main(String[]args) {
int[] array1 = {1, 4, 1};
int [] array2= {4,1};
int cnt=0;
for(int i=0;i<array1.length;i++){
for(int j=0; j<array2.length;j++){
if(array1[i]==array2[j]){
cnt++;
}}
}
if (cnt==array1.length){
System.out.print("have same elements");
Exercise #03:
(maxeven.java)
Write a program to read elements of array of 10 digits (integers) and
find the largest number , sum of even numbers (∑) and count of even
numbers.
import java.util.Scanner;
class LLL {
public static void main(String[] args) {
int[] a = new int[10];
Scanner in = new Scanner(System.in);
System.out.println("Enter 3 digit …");
for(int i = 0; i<a.length; i++) {
a[i] = in.nextInt();
}
//==================Max========================
int max = 0;
for ( int j = 0; j < a.length; j++) {
if ( a[j] > max) {
max = a[j];
}}
System.out.println("Max is:" +max);
//=================Sum of Even=========================
int sum=0;
for ( int j = 0; j < a.length; j++) {
if ( a[j] %2==0) {
sum=sum+a[j];
}} System.out.println("Sum of even is:" +sum);
//=================Count of Even=========================
int count=0;
for ( int j = 0; j < a.length; j++) {
if ( a[j] %2==0) {
count++;
}}
System.out.println("Count of even is:" +count);
}}
Related documents