Results 1 to 4 of 4
Thread: I need help with a program
- 07-21-2009, 12:44 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
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
- 07-21-2009, 06:07 AM #2
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, stepbystep.
Here you must choose: where do you want to start?
- 07-24-2009, 09:03 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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);
}
}
- 07-25-2009, 03:30 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@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
-
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks