Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java 1 Help In this exercise we explore declaring, allocating and assigning values to arrays containing lists of data Create a Java project called Arr

Java 1 Help

  • In this exercise we explore declaring, allocating and assigning values to arrays containing lists of data
  • Create a Java project called Arr
  • Add the following method to the code:
    public static void print(int values[]) { for (int i = 0; i < values.length; i++) { System.out.print(values[i] + " "); } } 
  • Compile your code to make sure it has correct syntax.
  • Declare and initialize an array for a list of 10 integer scores:
    int scores[] = {90, 91, 92, 93, 94, 95, 96, 97, 98, 99}; 
  • After declaring and initializing the array, call the print() method using the code:
    System.out.println("Integer test scores:"); print(scores);
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like this:
    Integer test scores: 90 91 92 93 94 95 96 97 98 99 
  • Next, write a method, named addExtraCredit. This method uses a for loop to add 5 points to each test score in the scores array.
  • The method should have the following signature:
public static void addExtraCredit(int values[]) { //Write the body of the method here //Note: Do not print anything in this method //No System.out.print statement(s) go here }
  • Next, call this method, passing in the scores array.
  • Then, call the print method again to display the values, along with the message After adding extra credit:
  • Note: Do NOT print inside of the addExtraCredit method. Call the print method to print the array in main. AddExtraCredit should only add extra credit to the scores. It should not print anything inside the body of the method.
  • Another way to think of it: This method has one purpose only: to add extra credit
  • The print method has one purpose: to print the array
  • Structuring the program to have these two separate methods allows for more flexibility. I am free to add extra credit without having to print the array each time I do so. It is my choice of whether to call printArray to print or not after I have added extra credit.
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the new output should look like this:
    After adding extra credit: 95 96 97 98 99 100 101 102 103 104 
  • Declare and initialize an array of double values holding rainfall values (in inches) 23.4, 16.4, 18.9, and 52.7
  • Write another print() method with one parameter for the array of double values.
  • After declaring and initializing the array, call the print() method.
  • Note that you can re-use the name print for this second method. The compiler will know they are different methods because the parameters are different. This is called method overloading.
  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like this:
Integer test scores: 90 91 92 93 94 95 96 97 98 99 After adding extra credit: 95 96 97 98 99 100 101 102 103 104 Double rainfall in inches 23.4 16.4 18.9 52.7
  • Declare and allocate an array of char values and assign it the vowels a, e, i, o and u.
  • After declaring and initializing the array, write another print method to display it. Again, this method should be named print(). Then, call this method in main.

  • Compile and run the program to make sure you made the changes correctly. When you run the program, the output should look like this:
  • Integer test scores: 90 91 92 93 94 95 96 97 98 99 After adding extra credit: 95 96 97 98 99 100 101 102 103 104 Double rainfall in inches 23.4 16.4 18.9 52.7 Char vowels a e i o u

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

10. What is meant by a feed rate?

Answered: 1 week ago