Download presentation
Presentation is loading. Please wait.
Published bySolomon Merritt Modified over 8 years ago
1
Computer Programming Lab 6
2
Exercise 1
3
Sample Runs
4
Scanner input = new Scanner(System.in); System.out.print("Enter a, b and c: "); double a = input.nextInt(); double b = input.nextInt(); double c = input.nextInt(); double discriminant = ((b*b) - (4*a*c)); Source Code
5
Cont if (discriminant > 0){ double root = Math.pow(discriminant, 0.5); double ro1 = ((-b + root)/(2*a)); double ro2 = ((-b - root)/(2*a)); System.out.println("The roots are " + ro1 + " and "+ro2); }else if (discriminant == 0){ double ro = (-b/(2*a)); System.out.println("The root is " + ro); }else System.out.println("The Equation has no real roots.");
6
Exercise 2 (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1,..., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week.
7
Source Code Scanner input = new Scanner(System.in); System.out.print("Enter today’s day: "); int today = input.nextInt(); System.out.print("Enter the number of days elapsed since today: "); int elapsed = input.nextInt(); String nameDay;
8
Cont. if (today == 0) { nameDay = "Sunday"; } else if (today == 1) { nameDay = "Monday"; } else if (today == 2) { nameDay = "Tuesday"; } else if (today == 3) { nameDay = "Wednesday"; } else if (today == 4) { nameDay = "Thursday"; } else if (today == 5) nameDay = "Friday"; else nameDay = "Saturday";
9
Cont. int futureDay = (today + elapsed) % 7; String nameFuture; if (futureDay == 0) { nameFuture = "Sunday"; } else if (futureDay == 1) { nameFuture = "Monday"; } else if (futureDay == 2) { nameFuture = "Tuesday"; } else if (futureDay == 3) { nameFuture = "Wednesday"; } else if (futureDay == 4) { nameFuture = "Thursday"; } else if (futureDay == 5) nameFuture = "Friday"; else nameFuture = "Saturday"; System.out.println("Today is " + nameDay + " and the future day is " + nameFuture);
10
Exercise 3
11
Scanner input = new Scanner(System.in); System.out.print("Enter three integers: "); int number1 = input.nextInt(); int number2 = input.nextInt(); int number3 = input.nextInt(); if (number1 > number2) { int temp = number1; number1 = number2; number2 = temp; } Source Code
12
if (number2 > number3) { int temp = number2; number2 = number3; number3 = temp; } if (number1 > number2) { int temp = number1; number1 = number2; number2 = temp; } System.out.println("The sorted numbers are " + number1 + " " + number2 + " " + number3); Cont
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.