Lab 8

advertisement
Computer Programming
Lab 8
Exercise 1
(Use the &&, || and ^ operators) Write a program that prompts the user to enter
an integer and determines whether it is divisible by 5 and 6, whether it is divisible
by 5 or 6, and whether it is divisible by 5 or 6, but not both.
Here is a sample run of this program:
Source Code
package Exersise1;
import java.util.Scanner;
public class Exersise1{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int x = input.nextInt();
System.out.println(((x % 5 == 0)&& (x % 6 == 0 ))? x + " is divisible by 5 and 6? True " :
x + " is divisible by 5 and 6? false ");
System.out.println(((x % 5 == 0)||(x % 6 == 0 ))? x + " is divisible by 5 or 6? True " :
x + " is divisible by 5 or 6? false ");
System.out.println(((x % 5 == 0)^(x % 6 == 0 ))? x + " is divisible by 5 and 6 put not both? True " :
x + " is divisible by 5 and 6 put not both ? false ");}}
Exercise 2
Source
Code
package Exercise2;
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int computerNumber = (int)(Math.random() * 3);
System.out.print("scissor (0), rock (1), paper (2): ");
int userNumber = input.nextInt();
switch (computerNumber) {
case 0:
if (userNumber == 0)
System.out.print("The computer is scissor. You are scissor too. It is a draw");
else if (userNumber == 1)
System.out.print("The computer is scissor. You are rock. You won");
else if (userNumber == 2)
System.out.print("The computer is scissor. You are paper. You lost");
break;
case 1:
if (userNumber == 0)
System.out.print("The computer is rock. You are scissor. You lost");
else if (userNumber == 1)
System.out.print("The computer is rock. You are rock too. It is a draw");
else if (userNumber == 2)
System.out.print("The computer is rock. You are paper. You won");
break;
case 2:
if (userNumber == 0)
System.out.print("The computer is paper. You are scissor. You won");
else if (userNumber == 1)
System.out.print("The computer is paper. You are rock. You lost");
else if (userNumber == 2)
System.out.print("The computer is paper. You are paper too. It is a draw");
break;}
}}
Exercise 3
(Financials: currency exchange) Write a program that prompts the user to enter
the exchange rate from currency in U.S. dollars to Saudi riyal SR or vice . Prompt
the user to enter 0 to convert from U.S. dollars to Saudi riyal SR and 1 to convert
from Saudi riyal SR and U.S. dollars. Prompt the user to enter the amount in
U.S. dollars or Saudi riyal SR to convert it to Saudi riyal SR or U.S. dollars,
respectively. Here are the sample runs:
Source Code :
package exercise3;
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter The amount of monye ");
int amount=input.nextInt();
System.out.println("Select 0 to convert it to U.S. dollars , and 1 to convert it to Saudi riyal SR ");
int choice=input.nextInt();
double result;
switch (choice) {
case 0:
result= (amount/3.75);
System.out.printf ( "%d SR equal to %f $",amount,result);
break;
case 1:
result=(amount*3.75);
System.out.printf ( "%d $ equal to %f SR",amount,result);
break ; }
}
}
Download
Related flashcards

Compilers

49 cards

Create Flashcards