Hello talented programmers! I'll cut straight to the chase: I thought I wanted to get into programming, but I SUCK at it. I tried going to extra help, and I tried to learn things on my own, but I just CANNOT figure it out. I have 5 projects I could not complete that's due next Wednesday, and I seriously cannot fail this class. So, I was wondering if you guys could help me out. Thanks!
*I'm not trying to weasel my way out of doing my work. Everything I handed in, I got a 0 or 65. I am truly desperate guys. Thanks.
1. [Find the largest] Many of our programs will require reading several integers from the user and storing them in an array. In the next set of in-class lessons, you will learn an efficient method for doing this. For now, you can use the code given in the problem below.
Complete and test the following code. Your program should read 3 integers from the user, store them in an array, and then print the largest of the three values. If there is a tie for the greatest value, just print that value.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[3];
//Read 3 integers from the user and store in an array
System.out.print("Enter 3 integers: ");
array[0] = input.nextInt();
array[1] = input.nextInt();
array[2] = input.nextInt();
//TYPE YOUR CODE HERE
}
When run, you should get results like:
Enter 3 integers: 7 23 11
Largest value is 23
Enter 3 integers: 18 3 18
Largest value is 18
2. [Lucas numbers] In mathematics, Lucas numbers are values that form a sequence such that each number is the sum of the previous two numbers. The most famous sequence of Lucas numbers are the Fibonacci numbers, which start with 1 and 1:
1 1 2 3 5 8 13 21 34 55 89 144 …
However, Lucas numbers can start with any two values, such as 1 and 3:
1 3 4 7 11 18 29 47 76 123 …
Write a program which starts by creating an array that can hold 6 integers. Then, read two integers from the user and place them in index locations 0 and 1 of the array. Next, fill the final 4 spaces in the array with the next four Lucas numbers, according to the rule above. Finally, print your array (using the print function given at the beginning of this assignment). Here is some example output:
Enter first number: 2
Enter second number: 5
2 5 7 12 19 31
3. [Rotate an array] Write a program which reads 3 numbers from the user and stores them in an array. Then, print the array. Next, move the first element of the array to the end of the array, moving all other elements left one space. To do this, you'll need to use a version of the "swapping" algorithm described in class. Print the array again. Rotate the elements once again (you can copy-and-paste your code). Finally, print the array one more time. Sample output:
Enter 3 numbers: 8 2 13
8 2 13
2 13 8
13 8 2
4. [Inventory simulator] You run a store which sells three products:
1. cheeseburgers
2. spaghetti
3. blankets
You're going to write a program which simulates keeping track of how many of each you have in stock. Here is some sample output:
Welcome to Wally-Mart!
We currently have:
1. Cheeseburgers – 247
2. Spaghetti - 182
3. Blankets - 27
Enter your order by number (1-3): 2
We now have:
1. Cheeseburgers – 247
2. Spaghetti - 181
3. Blankets - 27
Here’s how the order of things in your code should go:
1. Create an array using an array initializer, storing all 3 of the starting inventory numbers.
2. Print welcome
3. Print inventory
4. Ask for order from customer
5. Decrement the appropriate element of the array
6. Print the inventory again (you can copy-and-paste your code from above)
Step 4 is important. You must change the appropriate number in the array. You may not simply print one less than the number in the array. The code for steps 3 and 6 must be identical.
For step 5, do not use an else-if tree. Use the number that the user types as the index to the array. Just remember that item #1 is stored in location index 0, item #2 is in index 1, etc. As a hint, understand how the following code works (it isn’t exactly what you need to do, but it’s similar:
System.out.print(“Enter a number: “);
int num = input.nextInt();
//increment space in the array 2 to the right of location (num):
array[num+2] = array[num+2] + 1;
After you have your code working, change the numbers in step 1 above, and make sure your code still works.