Add some functionality to the code to demonstrate better a program that explains and allows the user
Question:
Add some functionality to the code to demonstrate better a program that explains and allows the user to understand all 10 concepts of Java, which are
1. Strings and at least one other data type.
2. Repetition Statements (including nested loops).
3. Switch Statement (menu of choices is required).
4. Arrays (including an array of objects).
5. Exceptions Handling.
6. Classes.
7. Methods (both static and instance, minimum 10 methods total). *Reference:
Lab 6A's 2nd Task.
8. Constructors (minimum two constructors and usage of the reserved keyword
this).
9. Mathematical algorithm/formula implementation (minimum 2, prebuilt
static methods can be used to satisfy this requirement).
10. Files and I/O
my code
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FinalProject {
private static Scanner scanner = new Scanner(System.in);
private static final String FILE_NAME = "data.txt";
public static void main(String[] args) {
FinalProject program = new FinalProject();
program.start();
}
private void start() {
boolean running = true;
while (running) {
printMenu();
int choice = getChoice();
switch (choice) {
case 1:
inputStrings();
break;
case 2:
repeatStatements();
break;
case 3:
performSwitch();
break;
case 4:
processArrays();
break;
case 5:
handleExceptions();
break;
case 6:
manipulateClasses();
break;
case 7:
performMethods();
break;
case 8:
handleFilesIO();
break;
case 9:
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
private void printMenu() {
System.out.println("=== MENU ===");
System.out.println("1. Input Strings and another data type");
System.out.println("2. Repeat Statements");
System.out.println("3. Switch Statement");
System.out.println("4. Arrays");
System.out.println("5. Exceptions Handling");
System.out.println("6. Classes");
System.out.println("7. Methods");
System.out.println("8. Files and I/O");
System.out.println("9. Quit");
System.out.print("Enter your choice: ");
}
private int getChoice() {
int choice;
try {
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
} catch (InputMismatchException e) {
scanner.nextLine(); // Clear the input buffer
choice = -1; // Set an invalid choice
}
return choice;
}
private void inputStrings() {
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
System.out.print("Enter another data type (int): ");
int inputData = scanner.nextInt();
// Perform operations with the input data
System.out.println("Entered String: " + inputString);
System.out.println("Entered Data: " + inputData);
}
private void repeatStatements() {
System.out.print("Enter the number of times to repeat: ");
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
System.out.println("Repetition " + (i + 1));
}
}
private void performSwitch() {
System.out.println("Choose an option:");
System.out.println("1. Option 1");
System.out.println("2. Option 2");
System.out.println("3. Option 3");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Option 1 selected.");
break;
case 2:
System.out.println("Option 2 selected.");
break;
case 3:
System.out.println("Option 3 selected.");
break;
default:
System.out.println("Invalid choice.");
break;
}
}
private void processArrays() {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Array length: " + numbers.length);
System.out.print("Array elements: ");
for (int number : numbers) {
System.out.print(number + " ");
}
System.out.println();
// Array of objects
String[] names = {"John", "Jane", "Bob"};
System.out.print("Names: ");
for (String name : names) {
System.out.print(name + " ");
}
System.out.println();
}
private void handleExceptions() {
System.out.print("Enter a number: ");
try {
int number = scanner.nextInt();
System.out.println("Entered number: " + number);
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.nextLine(); // Clear the input
}
}
private void manipulateClasses() {
Person person = new Person("Christian Bushart", 21);
System.out.println("Person name: " + person.getName());
System.out.println("Person age: " + person.getAge());
person.birthday();
System.out.println("Person age after birthday: " + person.getAge());
}
private void performMethods() {
int result = addNumbers(5, 10);
System.out.println("Result of adding numbers: " + result);
double average = calculateAverage(4, 7, 2);
System.out.println("Average of numbers: " + average);
}
private int addNumbers(int a, int b) {
return a + b;
}
private double calculateAverage(int... numbers) {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return (double) sum / numbers.length;
}
private void handleFilesIO() {
try {
File file = new File(FILE_NAME);
FileWriter writer = new FileWriter(file);
writer.write("Hello, World!");
writer.close();
System.out.println("Data written to file: " + file.getAbsolutePath());
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
}
try {
File file = new File(FILE_NAME);
Scanner fileScanner = new Scanner(file);
String data = fileScanner.nextLine();
fileScanner.close();
System.out.println("Data read from file: " + data);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
private static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void birthday() {
age++;
}
}
}
Intermediate Algebra
ISBN: 9780134895987
13th Edition
Authors: Margaret Lial, John Hornsby, Terry McGinnis