11 Replies - 374 Views - Last Post: 24 March 2019 - 12:16 PM Rate Topic: -----

#1 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Need help accepting the other arrays in my code

Posted 24 March 2019 - 09:45 AM

Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:

• getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array.
• GetAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array.
• GetHighest. This method should accept a one-dimensional array as its argument and return the highest of the values in the array.
• GetLowest. This method should accept a one-dimensional array as its argument and return the lowest of the values in the array.

Demonstrate each of the methods in the program using the data from the following four one-dimensional arrays.

// Some arrays of various types.
int[] iarray = { 2, 1, 9, 7, 3 };
float[] farray = { 3.5F, 4.6F, 1.7F, 8.9F, 2.1F };
double[] darray = { 98.7, 89.2, 55.1, 77.6, 99.9 };
long[] larray = {100, 500, 200, 300, 400 };

so far this is my code but I really don't know how to add the other arrays maybe im overlooking it and its frustrating me.

import java.util.Scanner;

public class RodriguezPass8 
{
   public static void main(String[] args) {
   final int ARRAY_SIZE = 5;
   int[] numbers = new int[ARRAY_SIZE];
   getValues(numbers);
   getTotal(numbers);
   getAverage(numbers);
   getHighest(numbers);
   getLowest(numbers);
   }

      public static int getTotal(int[] x) {
      int total = 0;
      for (int index = 0; index < x.length; index++) {
      total += x[index];
      }
         System.out.println("The total is " + total);
         return total;
         }

            public static double getAverage(int[] x) {
            int total = 0;
            for (int index = 0; index < x.length; index++) {
            total += x[index];
            }
               double average = (total / 2.0);
               System.out.println("The average is " + average);
               return average;
               }

                  public static int getHighest(int[] x) {
                  int highest = x[0];
                  for (int index = 1; index < x.length; index++) {
                  if (x[index] > highest)
                  highest = x[index];
                  }
                     System.out.println("The highest value is " + highest);
                     return highest;
                     }
   
                        public static int getLowest(int[] x) {
                        int lowest = x[0];
                        for (int index = 1; index < x.length; index++) {
                        if (x[index] < lowest)
                        lowest = x[index];
                        }
                           System.out.println("The lowest value is " + lowest);
                           return lowest;
                           }
      
                              private static void getValues(int[] array) {
                              Scanner keyboard = new Scanner(System.in);
                              System.out.println("Enter a series of " + array.length + " numbers.");
   
                              for (int index = 0; index < array.length; index++) {
                              System.out.print("Enter number " + (index + 1) + ": ");
                              array[index] = keyboard.nextInt();
                              }
               }
}


sorry this is my code with highlights

import java.util.Scanner;

public class RodriguezPass8 
{
   public static void main(String[] args) {
   final int ARRAY_SIZE = 5;
   int[] numbers = new int[ARRAY_SIZE];
   getValues(numbers);
   getTotal(numbers);
   getAverage(numbers);
   getHighest(numbers);
   getLowest(numbers);
   }

      public static int getTotal(int[] x) {
      int total = 0;
      for (int index = 0; index < x.length; index++) {
      total += x[index];
      }
         System.out.println("The total is " + total);
         return total;
         }

            public static double getAverage(int[] x) {
            int total = 0;
            for (int index = 0; index < x.length; index++) {
            total += x[index];
            }
               double average = (total / 2.0);
               System.out.println("The average is " + average);
               return average;
               }

                  public static int getHighest(int[] x) {
                  int highest = x[0];
                  for (int index = 1; index < x.length; index++) {
                  if (x[index] > highest)
                  highest = x[index];
                  }
                     System.out.println("The highest value is " + highest);
                     return highest;
                     }
   
                        public static int getLowest(int[] x) {
                        int lowest = x[0];
                        for (int index = 1; index < x.length; index++) {
                        if (x[index] < lowest)
                        lowest = x[index];
                        }
                           System.out.println("The lowest value is " + lowest);
                           return lowest;
                           }
      
                              private static void getValues(int[] array) {
                              Scanner keyboard = new Scanner(System.in);
                              System.out.println("Enter a series of " + array.length + " numbers.");
   
                              for (int index = 0; index < array.length; index++) {
                              System.out.print("Enter number " + (index + 1) + ": ");
                              array[index] = keyboard.nextInt();
                              }
               }
}


This post has been edited by NormR: 24 March 2019 - 09:55 AM
Reason for edit:: Moved ending code tag after code


Is This A Good Question/Topic? 0
  • +

Replies To: Need help accepting the other arrays in my code

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 09:54 AM

Quote

how to add the other arrays

Please list the arrays you are asking about.
Also please explain what problems you are having trying to add them.

Note: the get methods return values are being ignored by the calling code. The caller needs to assign the returned value to a variable:
   someVariable = getSomething(theArgs); 


The nesting of the source code needs fixing. The methods should be at the same indentation level, not indented as you go down the source.

This post has been edited by NormR: 24 March 2019 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 10:08 AM


sorry what I am trying to do is add the following arrays to get the average

int[] iarray = { 2, 1, 9, 7, 3 }; I already did this one in my code and need the following ones at the bottom
float[] farray = { 3.5F, 4.6F, 1.7F, 8.9F, 2.1F };
double[] darray = { 98.7, 89.2, 55.1, 77.6, 99.9 };
long[] larray = {100, 500, 200, 300, 400 };
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 10:13 AM

Quote

need the following ones at the bottom

Can you explain what problems you are having adding those array definitions?

If there are any error messages, copy the full text and paste it here/
Was This Post Helpful? 0
  • +
  • -

#5 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 10:59 AM

The problem I am having is I am not sure as how to add these array definitions for example:
   {
   final int ARRAY_SIZE = 5;
   int[] numbers = new int[ARRAY_SIZE];
   getValues(numbers);
   getTotal(numbers);
   getAverage(numbers);
   getHighest(numbers);
   getLowest(numbers);
   }
      {
         final float FLOAT_SIZE = 5;
         float[] numbers = new float[FLOAT_SIZE];
         getValues(numbers);
         getTotal(numbers);
         getAverage(numbers);
         getHighest(numbers);
         getLowest(numbers);
         }



the errors I get is: RodriguezPass8.java:17: error: incompatible types: possible lossy conversion from float to int
float[] numbers = new float[FLOAT_SIZE];
^
RodriguezPass8.java:18: error: incompatible types: float[] cannot be converted to int[]
getValues(numbers);
^
RodriguezPass8.java:19: error: incompatible types: float[] cannot be converted to int[]
getTotal(numbers);
^
RodriguezPass8.java:20: error: incompatible types: float[] cannot be converted to int[]
getAverage(numbers);
^
RodriguezPass8.java:21: error: incompatible types: float[] cannot be converted to int[]
getHighest(numbers);
^
RodriguezPass8.java:22: error: incompatible types: float[] cannot be converted to int[]
getLowest(numbers);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
6 errors
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 11:02 AM

Quote

incompatible types: float[] cannot be converted to int[]
getValues(numbers);

If you are working with primitive data types,
There needs to be a set a methods for each data type.
Was This Post Helpful? 0
  • +
  • -

#7 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 11:30 AM

if its okay do you mind showing me an example of how to do that? I am not that great with methods
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 11:37 AM

Quote

showing me an example

Take one of your methods and replace the int with float for the float array,
or replace the int with double for the double array
etc
Was This Post Helpful? 0
  • +
  • -

#9 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 11:41 AM

gotcha and then do I just add a { to separate them but will I have to input new string names or since they are within the { I can use the same?
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 11:49 AM

Methods can have the same name as long as the arguments to the method are unique. It is called method overloading.
See the tutorial: https://docs.oracle....OO/methods.html
Was This Post Helpful? 0
  • +
  • -

#11 crodz50   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 19

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 12:05 PM

so I did the methods thing and for some reason I keep getting error codes
these are the codes:

RodriguezPass8.java:72: error: illegal start of expression
public static float get_Total(float[] x)
^
RodriguezPass8.java:72: error: illegal start of expression
public static float get_Total(float[] x)
^
RodriguezPass8.java:72: error: ';' expected
public static float get_Total(float[] x)
^
RodriguezPass8.java:72: error: '.class' expected
public static float get_Total(float[] x)
^
RodriguezPass8.java:72: error: ';' expected
public static float get_Total(float[] x)
^
RodriguezPass8.java:83: error: illegal start of expression
public static double get_Average(float[] x)
^
RodriguezPass8.java:83: error: illegal start of expression
public static double get_Average(float[] x)
^
RodriguezPass8.java:83: error: ';' expected
public static double get_Average(float[] x)
^
RodriguezPass8.java:83: error: '.class' expected
public static double get_Average(float[] x)
^
RodriguezPass8.java:83: error: ';' expected
public static double get_Average(float[] x)
^
RodriguezPass8.java:95: error: illegal start of expression
public static float get_Highest(float[] x)
^
RodriguezPass8.java:95: error: illegal start of expression
public static float get_Highest(float[] x)
^
RodriguezPass8.java:95: error: ';' expected
public static float get_Highest(float[] x)
^
RodriguezPass8.java:95: error: '.class' expected
public static float get_Highest(float[] x)
^
RodriguezPass8.java:95: error: ';' expected
public static float get_Highest(float[] x)
^
RodriguezPass8.java:107: error: illegal start of expression
public static float get_Lowest(float[] x)
^
RodriguezPass8.java:107: error: illegal start of expression
public static float get_Lowest(float[] x)
^
RodriguezPass8.java:107: error: ';' expected
public static float get_Lowest(float[] x)
^
RodriguezPass8.java:107: error: '.class' expected
public static float get_Lowest(float[] x)
^
RodriguezPass8.java:107: error: ';' expected
public static float get_Lowest(float[] x)
^
RodriguezPass8.java:119: error: illegal start of expression
private static void get_Values(float[] array)
^
RodriguezPass8.java:119: error: illegal start of expression
private static void get_Values(float[] array)
^
RodriguezPass8.java:119: error: ';' expected
private static void get_Values(float[] array)
^
RodriguezPass8.java:119: error: '.class' expected
private static void get_Values(float[] array)
^
RodriguezPass8.java:119: error: ';' expected
private static void get_Values(float[] array)
^
25 errors
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • Posts: 5,762
  • Joined: 25-December 13

Re: Need help accepting the other arrays in my code

Posted 24 March 2019 - 12:16 PM

Quote

I keep getting error

Where are those statements with the errors?
You can not define a method inside of another method.
Method definitions must be outside of the {}s that contain another method's code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1