Question

In: Computer Science

please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...

please write the java code so it can run on jGRASP Thanks!
1 /**
 2  * PassArray
 3  * @Sherri Vaseashta
 4  * @Version 1
 5  * @see
 6  */
 7 import java.util.Scanner;
 8
 9 /**
  1. 10     This program demonstrates passing an array
    
  2. 11     as an argument to a method
    

12 */13

14 public class PassArray
15 {
16    public static void main(String[] args)
17    {
18
  1. 19        final int ARRAY_SIZE = 4;  //Size of the array
    
  2. 20        // Create an array.
    
  3. 21        int[] numbers = new int[ARRAY_SIZE];
    

22

  1. 23        // Pass the array to the getValues method
    
  2. 24        getValues(numbers);
    
25
26       System.out.println("Here are the numbers that you
entered:");
27
28
29
30
31
32
33
34
35
36    /**
  1. 37        The getValues method accepts a reference
    
  2. 38        to an array as its argument. The user is
    
  3. 39        asked to enter a value for each element.
    
  4. 40        @param array A reference to the array.
    
41    */
42
43    private static void getValues(int[] array)
44    {
  1. 45        //Create a Scanner object for keyboard input
    
  2. 46        Scanner keyboard = new Scanner(System.in);
    

47

// Pass the array to the showArray method
showArray(numbers);
// Pass the array to the calcAverage method
   calcAverage(numbers);
}//end of main
48       System.out.println("Enter a series of " + array.length
+ " numbers:");
49
  1. 50        //Read the values into the array
    
  2. 51        for (int index = 0; index < array.length; index++)
    
52       {
53          System.out.println("Enter number " + (index + 1) +
":");
54          array[index] = keyboard.nextInt();
55       }
56    }//end of getValues() method
57

58 /**

  1. 59        The showArray method accepts an array as
    
  2. 60        an argument and displays its contents.
    
  3. 61        @param array A reference to the array
    
62    */
63
64    public static void showArray(int[] array)
65    {
  1. 66        //Display the array elements.
    
  2. 67        for (int index = 0; index < array.length; index++)
    
68       {
69          System.out.println(array[index] + " ");
70       }
71
72    } //end of showArray() method
73
74    /**
  1. 75        The calcAverage method accepts an array as
    
  2. 76        an argument and calculates the sum of its
    
  3. 77        elements and then calculates the average of
    
  4. 78        its elements
    
  5. 79        @param array A reference to the array
    
80    */
81
82    public static void calcAverage(int[] array)
83    {
  1. 84 int sum = 0;

  2. 85        double average = 0;
    
  3. 86        //Sum the array elements.
    
  4. 87        for (int index = 0; index < array.length; index++)
    
88       {
89          sum = sum + array[index];
90       }
91       average = (double)sum / array.length;
92
93       System.out.println("Sum: " +  sum);
94       System.out.println("Average: " + average);
95    }//end of calcAverage() method
96
97 }//end of class

98

CODE 2

1 /**
 2  * TemperaturesMultidimensionalArrays
 3  * @Sherri Vaseashta
 4  * @Version1
 5  * @see
 6  */
 7
 8 import java.util.Scanner;
 9 public class TemperaturesMultidimensionalArrays
10 {
11    public static void main(String[] args)
12    {
13
  1. 14        Scanner keyboard = new Scanner(System.in);
    
  2. 15        int numDays = 0;
    
  3. 16        String[] dates;
    
  4. 17        double[][] temperatures;
    
  5. 18 int row = 0;

  6. 19 int col = 0;

20
21       System.out.println("This is the Temperature Stats
Program.");
22       System.out.println("It allows you to analyze
temperatures over a period of time.");
23       System.out.println("How many days do you want to track
temperatures?");
  1. 24        numDays = keyboard.nextInt();
    
  2. 25        keyboard.nextLine(); //needed any time you input a
    
String or char after a double|int
26
  1. 27        dates = new String[numDays];
    
  2. 28        temperatures = new double[numDays][3];
    

29

  1. 30        //load (read) the data into the arrays
    
  2. 31        for (row = 0; row < numDays; row++)
    
32       {
33          System.out.println("Please enter the date: (ex. Oct.
12)");
  1. 34           dates[row] = keyboard.nextLine();
    
  2. 35           System.out.println("Please enter the low for " +
    
dates[row]);
  1. 36           temperatures[row][0] = keyboard.nextDouble();
    
  2. 37           System.out.println("Please enter the high for " +
    
dates[row]);
  1. 38           temperatures[row][1] = keyboard.nextDouble();
    
  2. 39           temperatures[row][2] = (temperatures[row][0] +
    
temperatures[row][1]) / 2;
40          keyboard.nextLine();
41       }
42
43       //print the contents of the arrays to see if the data
is there. This is okay for a few columns
44       System.out.printf("%-20s%20s%20s%20s\n", "Dates",
"Lows", "Highs", "Average Temps");
45       for (row = 0; row < numDays; row++)
46       {
47          System.out.printf("%-20s%20.1f%20.1f%20.1f\n",
dates[row], temperatures[row][0], temperatures[row][1],
temperatures[row][2]);
48       }
49
50       //print the contents of the arrays to see if the data
is there. This is how to do it at WORK.
51       //You have to use nested loops when you have more than
a few columns
52       System.out.printf("\n\n%-20s%20s%20s%20s\n", "Dates",
"Lows", "Highs", "Average Temps");
53       for (row = 0; row < numDays; row++)
54       {
  1. 55           System.out.printf("%-20s", dates[row]);
    
  2. 56           for (col = 0; col < 3; col++)
    
57          {
58             System.out.printf("%20.1f",
temperatures[row][col]);
59          }
60          System.out.println();
61       }
62
63
64
65    }//end of main
66 }// end of class

Solutions

Expert Solution

Below is the solution:

public class PassArray {
   public static void main(String[] args) {
       final int ARRAY_SIZE = 4; // Size of the array
       // Create an array.
       int[] numbers = new int[ARRAY_SIZE];
       // Pass the array to the getValues method
       getValues(numbers);
       System.out.println("Here are the numbers that youentered:");
       // Pass the array to the showArray method
       showArray(numbers);
       // Pass the array to the calcAverage method
       calcAverage(numbers);
   } //end of main

   private static void getValues(int[] array) {
       // Create a Scanner object for keyboard input
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter a series of " + array.length + " numbers:");
       // Read the values into the array
       for (int index = 0; index < array.length; index++) {
           System.out.println("Enter number " + (index + 1) + ":");
           array[index] = keyboard.nextInt();
       }
   }
   // end of getValues() method

   public static void showArray(int[] array) {
       // Display the array elements.
       for (int index = 0; index < array.length; index++) {
           System.out.println(array[index] + " ");
       }
   } // end of showArray() method

   /*
   * 75 The calcAverage method accepts an array as 76 an argument and calculates
   * the sum of its 77 elements and then calculates the average of 78 its elements
   * 79 @param array A reference to the array
   */

   public static void calcAverage(int[] array) {
       int sum = 0;
       double average = 0;
       // Sum the array elements.
       for (int index = 0; index < array.length; index++) {
           sum = sum + array[index];
       }
       average = (double) sum / array.length;
       System.out.println("Sum: " + sum);
       System.out.println("Average: " + average);
   }// end of calcAverage() method
}// end of class

Sample output:

Enter a series of 4 numbers:
Enter number 1:
3452
Enter number 2:
4564
Enter number 3:
8756
Enter number 4:
3232
Here are the numbers that youentered:
3452
4564
8756
3232
Sum: 20004
Average: 5001.0

import java.util.Scanner;
public class TemperaturesMultidimensionalArrays {
   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);
       int numDays = 0;
       String[] dates;
       double[][] temperatures;
       int row = 0;
       int col = 0;
       System.out.println("This is the Temperature StatsProgram.");
       System.out.println("It allows you to analyze temperatures over a period of time.");
       System.out.println("How many days do you want to track temperatures?");
       numDays = keyboard.nextInt();
       keyboard.nextLine(); // needed any time you input a String or char after a double|int
       dates = new String[numDays];
       temperatures = new double[numDays][3];
       // load (read) the data into the arrays
       for (row = 0; row < numDays; row++) {
           System.out.println("Please enter the date: (ex. Oct.12)");
           dates[row] = keyboard.nextLine();
           System.out.println("Please enter the low for " + dates[row]);
           temperatures[row][0] = keyboard.nextDouble();
           System.out.println("Please enter the high for " + dates[row]);
           temperatures[row][1] = keyboard.nextDouble();
           temperatures[row][2] = (temperatures[row][0] + temperatures[row][1]) / 2;
           keyboard.nextLine();
       }

       // print the contents of the arrays to see if the data is there. This is okay
       // for a few columns
       System.out.printf("%-20s%20s%20s%20s\n", "Dates", "Lows", "Highs", "Average Temps");
       for (row = 0; row < numDays; row++) {
           System.out.printf("%-20s%20.1f%20.1f%20.1f\n", dates[row], temperatures[row][0], temperatures[row][1],
                   temperatures[row][2]);
       }

       // print the contents of the arrays to see if the data is there. This is how to
       // do it at WORK.
       // You have to use nested loops when you have more than a few columns
       System.out.printf("\n\n%-20s%20s%20s%20s\n", "Dates", "Lows", "Highs", "Average Temps");
       for (row = 0; row < numDays; row++) {
           System.out.printf("%-20s", dates[row]);
           for (col = 0; col < 3; col++) {
               System.out.printf("%20.1f", temperatures[row][col]);
           }
           System.out.println();
       }
   }// end of main
}// end of class

Sample output:

This is the Temperature StatsProgram.
It allows you to analyze temperatures over a period of time.
How many days do you want to track temperatures?
3
Please enter the date: (ex. Oct.12)
oct.12
Please enter the low for oct.12
12
Please enter the high for oct.12
60
Please enter the date: (ex. Oct.12)
Jan.10
Please enter the low for Jan.10
10
Please enter the high for Jan.10
40
Please enter the date: (ex. Oct.12)
Feb.14
Please enter the low for Feb.14
19
Please enter the high for Feb.14
60
Dates                               Lows               Highs       Average Temps
oct.12                              12.0                60.0                36.0
Jan.10                              10.0                40.0                25.0
Feb.14                              19.0                60.0                39.5


Dates                               Lows               Highs       Average Temps
oct.12                              12.0                60.0                36.0
Jan.10                              10.0                40.0                25.0
Feb.14                              19.0                60.0                39.5


Related Solutions

please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
Please write this program in JGRASP thanks you so much This program assesses your ability to...
Please write this program in JGRASP thanks you so much This program assesses your ability to use functions, arrays, for loops, and if statements where needed.You are writing a program to track bird sightings in Prince William Countyfor aweekend event where bird watchers watch birds and record each unique species that they see. Over a weekend, Saturday and Sunday, the bird watchers (aka birders) are going to record how many birdspeciesthey sighteach day. You begin by asking the user how...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT