Please write a program PrintLetters.java that prints the following matrix on the screen…
- User
Please write a program PrintLetters.java that prints the following matrix on the screen (Hint: you’ll need nested loops and other control structures. Try it from scratch only with the material you have from the lecture).
- Assistant
Sure! Here's the code for PrintLetters.java:
public class PrintLetters { public static void main(String[] args) { int numRows = 10; int numColumns = 10; char[][] matrix = new char[numRows][numColumns]; for(int i = 0; i < numRows; i++) { for(int j = 0; j < numColumns; j++) { if(i == 0 || i == numRows - 1 || i == j || i == numColumns - 1 - j) { matrix[i][j] = '*'; } else { matrix[i][j] = ' '; } } } for(int i = 0; i < numRows; i++) { for(int j = 0; j < numColumns; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }This program uses a 2D char array to represent the matrix. It loops through each row and column of the matrix and assigns the appropriate character ('*' for the edges and diagonals, and ' ' for everything else). Finally, it prints the characters on the screen to display the matrix.
- User
Please create a new class StockInfoCase.java to extend your BestBike program “MinimumStock” as follows:
- The program should not only calculate the MinimumStock for wheels, but also for handelbars and saddles. The user should be asked which component s/he wants to calculate – the selection should be possible by entering a number (e.g., wheels = 1, handlebars = 2, saddle =3) on the console.
- Depending on the user selection for component the user should be asked by the program to enter the input values (consumption and deliveryTime) for the respective component (hint: use a Switch Case control structure to differentiate between components).
- Management also decided to incorporate another variable into the calculation of minimum stock. They want to add a security factor that accounts for the volatility in delivery times for the different components. In the past, handlebar delivery has shown to be very volatile, so the security factor should increase the average delivery time by 50%. For saddles the factor is 20% and for wheels 0 (i.e. there is no delivery time volatility). The new formula is now: minStock = consumption * (deliveryTime * (1 + securityFactor)); (Hint: you will need to use Math.ceil() from java.lang.Math to calculate the end value for minStock – why?)
- Assistant
Sure! Here's the code for the StockInfoCase.java class:
import java.util.Scanner; public class StockInfoCase { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please specify the ID for the type of the component:"); System.out.println("Wheel: 1"); System.out.println("Handlebar: 2"); System.out.println("Saddle: 3"); System.out.print("ID: "); int componentId = scanner.nextInt(); double consumption, deliveryTime, securityFactor, minStock; switch(componentId) { case 1: System.out.print("Wheel Consumption per week: "); consumption = scanner.nextDouble(); System.out.print("Wheel Delivery Time: "); deliveryTime = scanner.nextDouble(); securityFactor = 0; break; case 2: System.out.print("Handlebar Consumption per week: "); consumption = scanner.nextDouble(); System.out.print("Handlebar Delivery Time: "); deliveryTime = scanner.nextDouble(); securityFactor = 0.5; break; case 3: System.out.print("Saddle Consumption per week: "); consumption = scanner.nextDouble(); System.out.print("Saddle Delivery Time: "); deliveryTime = scanner.nextDouble(); securityFactor = 0.2; break; default: System.out.println("Invalid component ID!"); return; } minStock = consumption * (deliveryTime * (1 + securityFactor)); minStock = Math.ceil(minStock); System.out.println("Minimal number of component in stock: " + minStock); } }