Three Separate Java Classes For Different Problems
Three separate Java classes for different problems
The following are three projects. Make three separate classes. Example You must submit three java files. No Zip file will be allowed. Problem 1 : Write a program that accepts an amount of money on deposit and a number of years it has been on deposit (years can have decimals). It will determine the interest to be paid on the deposit based on the following schedule: Time on Deposit Interest Rate ≥ 5 years 4.5% Less than 5 and ≥4 years 4% Less than 4 and ≥3 years 3.5% Less than 3 and ≥2 years 2.5% Less than 2 and ≥1 years 2% Less than 1 year 1.5% Compute the interest with the formula: Interest = Deposit IntRate Years. Display the original deposit, the interest earned, and the new Balance (with interest added to the deposit. Problem 2 : Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered, as well as the average of all the numbers entered. Assume the initial value of the smallest number and the largest number is -99 and the initial average is 0.0. Make sure the average prints out to two decimal places. Problem 3 For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), its roots are given by the formula:- The value of the discriminant ( b2-4ac) determines the nature of roots. Write a program that reads the values of a, b and c from the user and performs the following:- If the value of the discriminant is positive , the program should print out that the equation has two real roots and prints the values of the two roots. If the discriminant is equal to 0 , the roots are real and equal. if the value of the discriminant is negative , then the equation has two complex roots. Sample Output Enter values for a, b and c:- 2 10 3 Discriminant = 76 Equation has two real roots Roots are as follows:- x1 = -0.320551 x2 = -5.32055 Any character to end:-
Paper For Above instruction
Implementation of three Java programs for mathematical and data processing tasks
In this paper, three distinct Java programs are developed to address specific computational problems: calculating interest on a deposit with variable interest rates, analyzing a series of user-entered integers, and solving quadratic equations based on user-input coefficients. Each program exemplifies fundamental programming principles including user input handling, control flow, loops, conditionals, and mathematical computations, showcasing versatility in problem-solving techniques.
Program 1: Deposit Interest Calculator
The first program focuses on financial computations involving deposits, interest, and time. The objective is to accept user input for the deposit amount and the duration in years (including fractional years), determine the applicable interest rate based on the duration, compute the interest, and display the results including the new balance.
The program employs conditional statements to categorize the number of years into specific interest rates. It then applies the simple interest formula: Interest = Deposit Interest Rate Years. Using Scanner class for input, the program prompts the user to enter deposit and years, calculates interest, and displays the original deposit, interest earned, and the total balance after interest addition.
Code snippet:
import java.util.Scanner;
public class DepositInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter deposit amount: ");
double deposit = scanner.nextDouble();
System.out.print("Enter number of years (can be decimal): ");
double years = scanner.nextDouble();
double interestRate;
if (years >= 5) {
interestRate = 0.045;
} else if (years >= 4) {
interestRate = 0.04;
} else if (years >= 3) {
interestRate = 0.035;
} else if (years >= 2) {
interestRate = 0.025;
} else if (years >= 1) {
interestRate = 0.02;
} else {
interestRate = 0.015;
}
double interest = deposit interestRate years;
double newBalance = deposit + interest;
System.out.printf("Original deposit: %.2f%n", deposit);
System.out.printf("Interest earned: %.2f%n", interest);
System.out.printf("New balance: %.2f%n", newBalance);
scanner.close();
}
}
Program 2: Integer Series Analysis
The second program involves collecting a series of integers from the user until a sentinel value (-99) is entered. After input collection, it computes and displays the largest and smallest numbers, and the average of all entered numbers, formatted to two decimal places.
Data validation is handled by initializing smallest and largest values to -99, which is the sentinel. The program uses a loop with Scanner to read numbers, updating max, min, and total sum accordingly. After input ends, average is calculated and displayed.
import java.util.Scanner;
public class IntegerSeriesAnalysis {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int max = -99;
int min = -99;
int count = 0;
int sum = 0;
while (true) {
System.out.print("Enter an integer (-99 to end): ");
number = scanner.nextInt();
if (number == -99) {
break;
}
if (count == 0) {
max = number;
min = number;
} else {
if (number > max) max = number;
if (number
}
sum += number;
count++;
}
if (count > 0) {
double average = (double) sum / count;
System.out.println("Largest number entered: " + max);
System.out.println("Smallest number entered: " + min);
System.out.printf("Average of numbers: %.2f%n", average);
} else {
System.out.println("No numbers entered except sentinel.");
}
scanner.close();
}
}
Program 3: Quadratic Equation Roots Finder
The third program solves quadratic equations ax2 + bx + c = 0 based on user inputs for coefficients. It calculates the discriminant (b2 - 4ac) and determines the nature of roots—real and distinct, real and equal, or complex—and accordingly displays the roots.
The program performs input validation, computes the discriminant, and uses conditionals to decide the type of roots. For real roots, the quadratic formula is used; for complex roots, the roots are expressed as complex conjugates.
import java.util.Scanner;
public class QuadraticRoots {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter values for a, b, and c:");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double discriminant = b b - 4 a * c;
System.out.println("Discriminant = " + discriminant);
if (discriminant > 0) {
double sqrtDiscriminant = Math.sqrt(discriminant);
double root1 = (-b + sqrtDiscriminant) / (2 * a);
double root2 = (-b - sqrtDiscriminant) / (2 * a);
System.out.println("Equation has two real roots");
System.out.printf("x1 = %.6f%n", root1);
System.out.printf("x2 = %.6f%n", root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Equation has two real and equal roots");
System.out.printf("Root = %.6f%n", root);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("Equation has two complex roots");
System.out.printf("x1 = %.6f + %.6fi%n", realPart, imaginaryPart);
System.out.printf("x2 = %.6f - %.6fi%n", realPart, imaginaryPart);
}
scanner.close();
}
}
References
- Deitel, P. J., & Deitel, H. M. (2014). Java How to Program. Pearson.
- Liang, Y. D. (2015). Introduction to Java Programming and Data Structures. Pearson.
- Gaddis, T. (2018). Starting Out with Java: From Control Structures through Data Structures. Pearson.
- Horstmann, C. S. (2018). Core Java Volume I–Fundamentals. Prentice Hall.
- Selvin, S. (2020). Practical Programming in Java. Springer.
- Kurniawan, A. L. (2019). Java Programming for Beginners. Packt Publishing.
- Java Documentation. Oracle. (2023). https://docs.oracle.com/en/java/
- Stack Overflow Community Discussions. (2023). https://stackoverflow.com/
- W3Schools Java Tutorial. (2023). https://www.w3schools.com/java/
- GeeksforGeeks. (2023). Java Programs. https://www.geeksforgeeks.org/java/