Java Programming Assignment 1
Java Programming Assignment 1
Use a loop to iterate 20 times, maintaining variables for previous two Fibonacci numbers and the current number. Start with initial values 0 and 1, then repeatedly add them to generate the next number in the sequence, updating the previous numbers each iteration. Print each number to the console as it is generated .
Input a month number from the user, 1 through 12. Implement a switch case structure where each case corresponds to a month and returns the number of days for that month. Include a default case for values outside the range to display an error message. Adjust for leap years in February using additional logic if the year is identified .
Create a class to store student information like studentCode, studentName, studentAge, and studentSex. Use a list or array to manage multiple students. Implement user input using a loop, prompting the user to enter details for each student until the 'n' key is pressed to stop. Input data should populate instances of the student class, and you should implement functionality to print the student list .
First, accept an integer input 'n' from the user. Initialize a long variable to 1, and use a loop to multiply this variable by each integer from 1 up to 'n'. This loops accordingly to calculate the factorial of 'n'. Output the resulting factorial value to the user .
First, gather inputs for 'a', 'b', and 'c' from the user. Calculate the discriminant using b^2 - 4ac. Depending on the discriminant value, calculate the roots: if positive, use the quadratic formula to compute two real roots; if zero, compute one real root; if negative, compute two complex roots. Output the calculated roots .
To find the greatest common divisor (GCD) of two numbers 'a' and 'b', you can use the Euclidean algorithm. Implement a recursive method 'int greatestCommonDivisor(int a, int b)' that returns 'b' when 'b' is zero, otherwise calls the method with parameters 'b' and 'a % b'. This efficiently calculates the GCD .
First, input values for 'a' and 'b' from the user. Check the condition that 'a' should not be zero to ensure it's a valid equation. If 'a' is not zero, solve for x using x = -b/a. Finally, output the value of x .
To swap two integer variables, say 'a' and 'b', using a third temporary variable 'c', you would perform the following steps: assign the value of 'a' to 'c', assign the value of 'b' to 'a', and finally assign the value of 'c' to 'b'. This effectively swaps the values of 'a' and 'b' .
Use a nested loop structure where the outer loop iterates over each row and the inner loop prints the required number of stars per row based on user input. For N rows of stars, each with N stars per row, iterate N times in both loops, printing a star followed by a newline character at the end of each inner loop .
First, create variables to store the three grades—math, physics, and chemistry. Calculate their average by dividing their sum by three. Then, use conditional statements to determine the rank based on the average: 'A' for average >= 8.0, 'B' for >= 6.5 and < 8.0, 'C' for >= 5.0 and < 6.5, and 'D' for averages below 5.0 .