java-4th-assignment sol

advertisement
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
Name
ID
Section Number
Teacher Name:
Assignment Policy:
1.
2.
3.
4.
5.
6.
Late assignments will NOT be accepted.
Cheating is forbidden in this course, and will be considered a zero mark.
Students are encouraged to collaborate and work into groups of two.
Please clearly write your name, section number, and student number on the cover page.
You should print the cover page with your sheet.
Assignments should be stapled or placed in an unsealed envelope.
(You should submit your sheet in Sunday,
8 December)
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
Q1: Triangle Type
Write a program with a method named computeTrianglePerimeter which
takes three float arguments entered by the user, as the three sides of a triangle.
The method must calculate and returns the perimeter of the triangle. In the
main() method, you should display the result, and one of the following
statements:
This is an Equilateral Triangle. (If the three sides are equals)
This is a Scalene Triangle. (If all sides are not equals)
This is an Isosceles Triangle. (Otherwise - If only two sides are equals)
Hint:
Triangle Perimeter = side1 + side2 + side3
Sample Outputs:
Pleas Enter the Three Triangle Sides: 3 3 3
The Perimeter of the Triangle = 9
This is an Equilateral Triangle.
Pleas Enter the Three Triangle Sides: 2 3 4
The Perimeter of the Triangle = 9
This is a Scalene Triangle.
Pleas Enter the Three Triangle Sides: 5 6 5
The Perimeter of the Triangle = 16
This is an Isosceles Triangle.
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
import java.util.Scanner;
public class Triangle {
public static float computeTrianglePerimeter(float side1 , float side2, float side3) {
return side1 + side2 + side1;
}
public static void main(String[] args) {
float side1 , side2, side3, perimeter;
System.out.print( "Pleas Enter the Three Triangle Sides: ") ;
Scanner input = new Scanner(System.in);
side1 = input.nextFloat();
side2 = input.nextFloat();
side3 = input.nextFloat();
perimeter = computeTrianglePerimeter(side1 , side2, side3);
System.out.println( "The Perimeter of the Triangle = " + perimeter );
if (side1 == side2 && side1 == side3)
System.out.println( "This is an Equilateral Triangle." );
else if (side1 != side2 && side1 != side3)
System.out.println( "This is a Scalene Triangle." );
else
System.out.println( "This is an Isosceles Triangle." );
}
}
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
Q2: Restaurant Order Menu
Write a program that works as an automatic ordering menu. The program will
display the meals available beside their prices. The user chooses all the meals
he/she wants to include in their order, and then choose Done to complete the
order and displays the total, or Cancel to cancel it.
The program should include a method named orderMenu() that takes no
arguments and returns the total message.
Sample Output:
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 1
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 3
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 4
Your total is: 28 S.R
Thank You for choosing our restaurant. Hope to see you again.
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
Sample Output:
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 1
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 3
****** Order menu******
1- Chicken Meal – 15 S.R
2- Beef Meal – 17 S.R
3- Vegetarian Meal – 13 S.R
4- Done
5- Cancel
Please enter your choice: 5
There is no completed order, hope to see you again.
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
import java.util.Scanner;
public class Resturant {
public static float orderMenu() {
float total = 0;
Scanner input = new Scanner (System.in);
int choice;
do{
System.out.println("****** Order menu****** ");
System.out.println("1- Chicken Meal – 15 S.R ");
System.out.println("2- Beef Meal – 17 S.R ");
System.out.println("3- Vegetarian Meal – 13 S.R ");
System.out.println("4- Done ");
System.out.println("5- Cancel ");
System.out.println("Please enter your choice: ");
choice = input.nextInt();
switch(choice) {
case 1: total+=15 ; break;
case 2: total+=17 ; break;
case 3: total+=13 ; break;
case 4: break;
case 5: total = 0; break;
default: System.out.println("Invalid choice, please enter a valid number: ");
}
} while (choice!= 4 && choice != 5);
return total;
}
PRINCESS NORA UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCES DEPARTMENT
CS141- JAVA language
H.W #4
1st Semester 1434-1435
public static void main(String[] args) {
float totalPrice = orderMenu();
if (totalPrice== 0 )
System.out.println("There is no completed order, hope to see you again.");
else
System.out.println("Your total price is: " + totalPrice +
" Thank You for choosing our restaurant. Hope to see you again.");
}
}
Download