Using a Single-Array, create a program that asks the user to input a day of the week, the high temperature, and the low temperature. Calculate the Average High and Low.

## All 22 Replies

You need to at least try to write the program. We'll help you, but we won't do it all for you. If you were awake during class or read some of the book, then you should have learned something about creating a class with a "main" method in it. Start there.

What Jeff said. You'll need to read into getting input, declaring variables (such as a String array), using arrays, and outputting results. We won't do your homework for you.

Sorry about that, didnt want to give that impression. Its been a while since I had to deal with Java. I have no problem coming up with a program to ask for the day and that day's high temp and then taking those 7 temp's and come up with an average. But cannot come up with a program to show the days, the high temp, and low temp in columns and at the bottome with the average of low and high. I need to have 23elememts, seven days, seven high's, seven low's, the word day, the word high, and the word low.

``````import java.util.*;

public class Temperature
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);

// Input the number of days from the user.
System.out.print("How many days' temperatures? ");
int days = console.nextInt( );

// Declare an array, maybe should check if days is positive
int[ ] temps = new int[days];

// Input and store the temperatures in the array
for (int i = 0; i < temps.length; i++)
{
System.out.print("Day " + i + "'s high temp: ");
temps[i] = console.nextInt( );
}

// Calculate and print the average
int sum = 0;
for (int i = 0; i < temps.length; i++)
{
sum += temps[i];
}
// need a cast to avoid integer division
double average = (double) sum / temps.length;
System.out.println("Average temp = " + average);

// Count the number of values that were above average
int count = 0;
for (int i = 0; i < temps.length; i++)
{
if (temps[i] > average)
{
count++;
}
}
System.out.println(count + " days were above average");
}
}``````

I cant figure out were these two fit in at.

``````// what is the name for each day of the week
String[ ] weekDayNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

// Sum the temperatures
int sum = 0;
for (int foo : temps) {
sum += foo;
}``````

See what you can come up with and ask specific questions about what you are having trouble with. What you posted above is a direct copy of code you found elsewhere, so that's not going to cut it.

So if I understand this properly, you're trying to end up with output that looks something like this:

``````Low   High
Sun   50    70
Mon   55    73
Tue   60    80
Wed   62    84
Thu   58    70
Fri   53    66
Sat   48    50
Avg 55.14 70.43``````

To do that, I'd probably use two 'int' arrays of seven elements each: One for "dailyLows" and one for "dailyHighs". Your array of weekday names would prove useful. You'd probably want to total fields -- one for lows and one for highs.

That should send you in the right direction.

This is what I came up with but I receive the error.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
TemperatureInfo cannot be resolved to a type
TemperatureInfo cannot be resolved to a type
TemperatureInfo cannot be resolved to a type
TemperatureInfo cannot be resolved to a type

``````import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

/**
* Class that asks user to input temperatures and calculates avg temperatures.
*/
public class  Weekly_Temperatures
{
private static final Scanner SCANNER = new Scanner(System.in);

public static void main(String[] args)
{
TemperatureInfo[] Weekly_Temperatures = new TemperatureInfo[7];
for (int i = 0; i < Weekly_Temperatures.length; i++)
{
System.out.print("Input day: ");
String day = SCANNER.nextLine();
System.out.print("High temperature: ");
float high = SCANNER.nextFloat();
System.out.print("Low temperature: ");
float low = SCANNER.nextFloat();
SCANNER.nextLine();
Weekly_Temperatures[i] = new TemperatureInfo(day, high, low);
}

System.out.printf("%10s%7s%7s\n", "DAY", "HIGH", "LOW");
for (TemperatureInfo temperatureInfo : Weekly_Temperatures)
{
System.out.printf("%10s%7s%7s\n", temperatureInfo.getDay(), temperatureInfo.getHigh(), temperatureInfo.getLow());
}
System.out.println();
System.out.printf("Average High: %.2f\n", calculateAvgHigh(Weekly_Temperatures));
System.out.printf("Average Low: %.2f",  calculateAvgLow(Weekly_Temperatures));
}

/**
* Calculates avg high temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgHigh(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getHigh();
}
return sum / temperatureInfos.length;
}

/**
* Calculates avg low temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgLow(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getLow();
}
return sum / temperatureInfos.length;
}
}``````

sorry but i dont know how to add code tags

So if I understand this properly, you're trying to end up with output that looks something like this:

``````Low   High
Sun   50    70
Mon   55    73
Tue   60    80
Wed   62    84
Thu   58    70
Fri   53    66
Sat   48    50
Avg 55.14 70.43``````

To do that, I'd probably use two 'int' arrays of seven elements each: One for "dailyLows" and one for "dailyHighs". Your array of weekday names would prove useful. You'd probably want to total fields -- one for lows and one for highs.

That should send you in the right direction.

Just to add : You can either use two arrays (One for highs and one for lows) or one two-dimensional array.

In the former solution (easier for beginners), the subscript of the first array that equals the second array should correspond to the same day.

Sample:

``````int[] tempHighs = new int[7];
int[] tempLows = new int[7];
/*
* tempHighs[0] and tempLows[0] both correspond to the day Monday. 1 - Tuesday, and so on.
*/``````

I'm not going to go through your entire code because quite frankly it's impossible to read; but here's an example of how to input values for the previous array initialization example:

``````for(int i = 0; i<7; i++)
{
System.out.print("Input high value for " +i +": ");
tempHighs[i] = scanner.nextInt();
System.out.print("Input low value for " +i +": ");
tempLows[i] = scanner.nextInt();
}``````

You should probably use a switch statement for i to display the actual day name, but I leave it up to you.

Whats wrong with this code?

``````**********************************************************************
*
*
*
*
* Date
*
* Assignment 1
*
***********************************************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class  Weekly_Temperatures
{
private static final Scanner SCANNER = new Scanner(System.in);

public static void main(String[] args)
{
TemperatureInfo[] Weekly_Temperatures = new TemperatureInfo[7];
for (int i = 0; i < Weekly_Temperatures.length; i++)
{
System.out.print("Input day: ");
String day = SCANNER.nextLine();
System.out.print("High temperature: ");
float high = SCANNER.nextFloat();
System.out.print("Low temperature: ");
float low = SCANNER.nextFloat();
SCANNER.nextLine();
Weekly_Temperatures[i] = new TemperatureInfo(day, high, low);
}

System.out.printf("%10s%7s%7s\n", "DAY", "HIGH", "LOW");
for (TemperatureInfo temperatureInfo : Weekly_Temperatures)
{
System.out.printf("%10s%7s%7s\n", temperatureInfo.getDay(), temperatureInfo.getHigh(), temperatureInfo.getLow());
}
System.out.println();
System.out.printf("Average High: %.2f\n", calculateAvgHigh(Weekly_Temperatures));
System.out.printf("Average Low: %.2f",  calculateAvgLow(Weekly_Temperatures));
}

/**
* Calculates avg high temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgHigh(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getHigh();
}
return sum / temperatureInfos.length;
}

/**
* Calculates avg low temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgLow(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getLow();
}
return sum / temperatureInfos.length;
}
}``````

Whats wrong with this code?

Code tags: Use the button that says "CODE" with square brackets around it.

Looks like you need to write the TemperatureInfo class. It's trivial. Eclipse mostly generated it for me. So I'll just include it below. (Click on the "Toggle Plain Text" link before copying it. ;-)

``````public class TemperatureInfo {

private final String day;
private final float high, low;

public TemperatureInfo(String day, float high, float low) {
this.day = day;
this.high = high;
this.low = low;
}

public String getDay() {
return day;
}

public float getHigh() {
return high;
}

public float getLow() {
return low;
}

}``````

If you don't know how to compile and use more than one class at a time, it is possible to put this class inside the other one. It's not really good form to do that, but it will do "in a pinch." To do that, change `public class TemperatureInfo` to `private static class TemperatureInfo` and copy it into the body of the Weekly_Temperatures class. (IE: Inside the class' brackets, but outside of any method body. I would put it before the final "}" of the file.)

Whats wrong with this code?

``````**********************************************************************
*
*
*
*
* Date
*
* Assignment 1
*
***********************************************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class  Weekly_Temperatures
{
private static final Scanner SCANNER = new Scanner(System.in);

public static void main(String[] args)
{
TemperatureInfo[] Weekly_Temperatures = new TemperatureInfo[7];
for (int i = 0; i < Weekly_Temperatures.length; i++)
{
System.out.print("Input day: ");
String day = SCANNER.nextLine();
System.out.print("High temperature: ");
float high = SCANNER.nextFloat();
System.out.print("Low temperature: ");
float low = SCANNER.nextFloat();
SCANNER.nextLine();
Weekly_Temperatures[i] = new TemperatureInfo(day, high, low);
}

System.out.printf("%10s%7s%7s\n", "DAY", "HIGH", "LOW");
for (TemperatureInfo temperatureInfo : Weekly_Temperatures)
{
System.out.printf("%10s%7s%7s\n", temperatureInfo.getDay(), temperatureInfo.getHigh(), temperatureInfo.getLow());
}
System.out.println();
System.out.printf("Average High: %.2f\n", calculateAvgHigh(Weekly_Temperatures));
System.out.printf("Average Low: %.2f",  calculateAvgLow(Weekly_Temperatures));
}

/**
* Calculates avg high temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgHigh(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getHigh();
}
return sum / temperatureInfos.length;
}

/**
* Calculates avg low temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgLow(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getLow();
}
return sum / temperatureInfos.length;
}
}``````

Whats wrong with this code?

Whats wrong with this code?

Oh, and by the way...
Your code has very good design. It's both object-oriented and modular. I haven't seen a beginner write such well-designed code in some time.

I only see one trivial thing wrong with the posted code: It needs a slash ("/") at the very beginning, to start the header comment. ;)

If you don't know how to compile and use more than one class at a time, it is possible to put this class inside the other one. It's not really good form to do that, but it will do "in a pinch."

Is it any way you could show me how?

'..., it is possible to put this class inside the other one. It's not really good form to do that, but it will do "in a pinch." '

Is it any way you could show me how?

Like this:

``````public class Weekly_Temperatures {
private static final Scanner SCANNER = new Scanner(System.in);

public static void main(String[] args) {
...
}
...
private static float calculateAvgLow(TemperatureInfo[] temperatureInfos) {
...
}

private static class TemperatureInfo {

private final String day;
private final float high, low;

public TemperatureInfo(String day, float high, float low) {
this.day = day;
this.high = high;
this.low = low;
}

public String getDay() {
return day;
}

public float getHigh() {
return high;
}

public float getLow() {
return low;
}

}

}``````

Tricks:

• You have to put it in the right place.
• It must be "static" because it is being used from a static method.
• It's good form to make it private, rather than public.

Using a Single-Array, create a program that asks the user to input a day of the week, the high temperature, and the low temperature. Calculate the Average High and Low. Each day, high temperature, low temperature, Average High and Low will be saved to an element in the single array called "Weekly_Temperatures". After all info is input and calculated, it will be displayed in the following format. You may use the printf if you like.

DAY HIGH LOW
Sunday ### ###
Monday ### ###
Tuesday ### ###
Wednesday ### ###
Thursday ### ###
Friday ### ###
Saturday ### ###

Average High = ###
Average Low = ###

``````/**********************************************************************
*
*
*
*
* Date
*
* Assignment 1
*
***********************************************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class  Weekly_Temperatures
{
private static class TemperatureInfo
{
private String day;
private float high;
private float low;

public TemperatureInfo(String day, float high, float low)
{
this.day = day;
this.high = high;
this.low = low;
}

public String getDay()
{
return day;
}

public float getHigh()
{
return high;
}

public float getLow()
{
return low;
}
}

private static final Scanner SCANNER = new Scanner(System.in);

public static void main(String[] args)
{
TemperatureInfo[] Weekly_Temperatures = new TemperatureInfo[7];
for (int i = 0; i < Weekly_Temperatures.length; i++)
{
System.out.print("Input day: ");
String day = SCANNER.nextLine();
System.out.print("High temperature: ");
float high = SCANNER.nextFloat();
System.out.print("Low temperature: ");
float low = SCANNER.nextFloat();
SCANNER.nextLine();
Weekly_Temperatures[i] = new TemperatureInfo(day, high, low);
}

System.out.printf("%10s%7s%7s\n", "DAY", "HIGH", "LOW");
for (TemperatureInfo temperatureInfo : Weekly_Temperatures)
{
System.out.printf("%10s%7s%7s\n", temperatureInfo.getDay(), temperatureInfo.getHigh(), temperatureInfo.getLow());
}
System.out.println();
System.out.printf("Average High: %.2f\n", calculateAvgHigh(Weekly_Temperatures));
System.out.printf("Average Low: %.2f",  calculateAvgLow(Weekly_Temperatures));
}

/**
* Calculates avg high temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgHigh(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getHigh();
}
return sum / temperatureInfos.length;
}

/**
* Calculates avg low temperature.
* @param temperatureInfos array of TemperatureInfo
* @return avg value
*/
private static float calculateAvgLow(TemperatureInfo[] temperatureInfos)
{
float sum = 0;
for (TemperatureInfo temperatureInfo : temperatureInfos)
{
sum += temperatureInfo.getLow();
}
return sum / temperatureInfos.length;

}

}``````

Does this Code answer that question?

I don't generally tell students if their answer satisfies the instructor's requirements. I let the students assess that question.

This is the output I'm getting:

``````DAY   HIGH    LOW
Sunday   70.0   50.0
Monday   73.0   55.0
Tuesday   80.0   60.0
Wednesday   84.0   62.0
Thusday   70.0   58.0
Friday   66.0   53.0
Saturday   50.0   48.0

Average High: 70.43
Average Low: 55.14``````

I understand, an thanks for your help. I'm just trying to pass the assignment, this class is killing me.

ok

> Does this Code answer that question?
If you can't tell, you need to stop wasting peoples' time here.

How am I wasting your time, I did the work. Just wanted your honest opinion.

Now I wouldn't be that harsh. We encourage beginners to come here and ask for help. We often have to help them towards asking questions that we can sensibly answer. Being ignorant and making mistakes are expected. That's how the site works.

The original question was off base. The first attempt was pretty lame. But the later work was really quite good.

Don't give up! Programming can be very frustrating. But it's doable, if you have patience and perseverance.

> But the later work was really quite good
I don't believe that the later work was his own any more than the first "attempt", which was directly copied from a book. He did not even have the TemperatureInfo class and merely copied yours into his program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.