LAB6

advertisement
Computer Programming
Lab 6
Exercise 1
(Algebra: solve quadratic equations) The two roots of a quadratic equation
ax2 + bx + c = 0 can be obtained using the following formula:
𝑟1 =
−𝑏+ 𝑏2 −4𝑎𝑐
2𝑎
and 𝑟2 =
−𝑏− 𝑏2 −4𝑎𝑐
2𝑎
b2 – 4ac is called the discriminant of the quadratic equation. If it is positive,
the equation has two real roots. If it is zero, the equation has one root. If it is
negative, the equation has no real roots.
Write a program that prompts the user to enter the values for a, b and c and
displays the result based on the discriminant. if the discriminant is positive,
display two roots. If the discriminant is 0, display one root. Otherwise, display
“The equation has no real roots”.
Sample Runs
Source Code
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));
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.");
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.
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;
Cont.
if (today == 0) {
else if (today == 3) {
nameDay = "Sunday";
nameDay = "Wednesday";
}
}
else if (today == 1) {
else if (today == 4) {
nameDay = "Monday";
nameDay = "Thursday";
}
}
else if (today == 2) {
else if (today == 5)
nameDay = "Tuesday";
}
nameDay = "Friday";
else
nameDay = "Saturday";
Cont.
int futureDay = (today + elapsed) % 7;
else if (futureDay == 3) {
String nameFuture;
nameFuture = "Wednesday";
}
if (futureDay == 0) {
else if (futureDay == 4) {
nameFuture = "Sunday";
nameFuture = "Thursday";
}
}
else if (futureDay == 1) {
else if (futureDay == 5)
nameFuture = "Monday";
}
nameFuture = "Friday";
else
else if (futureDay == 2) {
nameFuture = "Saturday";
nameFuture = "Tuesday";
}
System.out.println("Today is " + nameDay +
" and the future day is " + nameFuture);
Exercise 3
(Sort three integers) write a program that sorts three integers. The integers are entered from the
user and stored in variables num1, num2 and num3, respectively. The program sorts the number so
that 𝑛𝑢𝑚1 ≤ 𝑛𝑢𝑚2 ≤ 𝑛𝑢𝑚3.
Source Code
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;
}
Cont
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);
Download