Results 1 to 4 of 4
  1. #1
    DarkGranite is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default I need help with a program

    I am new to java, and i am taking a class in school. the professor wants us to write a program but i have no idea what i am doing.

    Here is what is asked of me:

    Write i program called temperatures.java that would prompt users to enter five (5) integer temperature values into an array of five integers. The program would then display back
    a. all values entered
    b. the sum of all numbers
    c. and their average with two decimal points.

    The program should be written with the class Temperatures, that would have the following data members:

    int[] temperatures = new int[5];
    int total;
    double average;

    and the following methods:

    public void getData(), that would prompt users to enter 7 temperature values
    public void computeSum(), that would sum up all the values and store the result into the total variable
    public void computeAverage(), that would compute the average and store the result into the average variable
    public void displayData(), that would display all the values from the array, the total and the average

    Write a separate program with the main function, called TemperaturesTest.java,
    that would call the methods getData(), computeSum(), computeAverage(), and displayData() from the class program Temperatures.java.
    Put the program into an indefinite loop until a "n" is entered at the prompt "Continue?[y/n]".

    please i need the help. badly

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You have two main areas to develop:
    1 — a class Temperatures that has the given data members, ie, member variables or fields, and the given methods
    2 — a test class, TemperaturesTest that has a main method in which you instantiate your Temperatures class and exercise it by calling its methods and accessing its data members, ie, in which you use the Temperatures class as an object.
    To allow the user to use this you build a loop in which you receive user input as long as the user wants to keep entering it and using the class.

    So you can start with either one you like and build it up. Write some code, compile and run, add more, compile and run ... until you get it the way you want. You can always go back and make changes as you progress.
    Then build the other class in the same way, step–by–step.
    Here you must choose: where do you want to start?

  3. #3
    javamadd is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Talking this is the program !!

    import java.util.*;
    public class temperature {

    /**
    * @author javamadd
    */
    public static void main(String[] args) {
    int[] tempe = new int[5]; // to store the five inputs from the user
    int total; // for total
    double average; // for average
    tempe =getData(); // calls getData() and returns the contents of the array to tempe[]
    total = computeSum(tempe); // passing the array tempe to function computeSum,it returns the solution to the variable total
    //System.out.println("the total is " + total);
    average = computeAverage(total); // pass total to ComputeAverage and it returns a double to average
    System.out.println("the average is " + average);
    displayData(tempe,total,average); // passing array ,int and a double to displayData

    }

    public static int[] getData(){ //returns the array to main
    int[] temperatures = new int[5];
    System.out.println("Enter the values");
    Scanner scanboy = new Scanner(System.in);
    for (int i=0;i<5;i++){
    temperatures[i]= scanboy.nextInt();
    }
    return temperatures;
    }


    public static int computeSum( int[] tempe){ // adding contents of array and saving it in 'total'
    System.out.println("the Entered values are ");
    int total=0;
    for (int i=0;i<5;i++){
    total = total + tempe[i];
    }
    return total;
    }
    public static double computeAverage(int total ){
    double avg = total/5;
    return avg;
    }
    public static void displayData(int[] tempe,int total,double avg){
    System.out.println("the values in the array are ");
    for (int i=0;i<5;i++){
    System.out.print( " "+ tempe[i]);
    }
    System.out.println("The Total is " + total + " and the average is " + avg);
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @javamadd, please don't do this next time lol. You are not helping to our thread tarter. In this way he/she not learning anything, because you just give a code away. Please in first step just give an idea what exactly want to do, as hardwired did in his last post. Good luck :)

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •