Beginner: need help
I am a beginner in Java.
I wrote this code to get temperature of the days of the week, get the coolest,
hotest and average. Everything looks fine, but when I run it I get wrong
values for the hottest and coolest day.
Can anybody tell me what is wrong?
Please!!!
import javax.swing.*;
import java.text.DecimalFormat;
public class Lab2Ex1
{
double daysOfWeek[] = new double[7];
String nameOfDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"};
public Lab2Ex1 (double inSun, double inMon, double inTues, double inWed,
double inThur, double inFri, double inSat)
{
daysOfWeek[0] = inSun;
daysOfWeek[1] = inMon;
daysOfWeek[2] = inTues;
daysOfWeek[3] = inWed;
daysOfWeek[4] = inThur;
daysOfWeek[5] = inFri;
daysOfWeek[6] = inSat;
}
public double getAvgTemp()
{
return (daysOfWeek[0] + daysOfWeek[1] + daysOfWeek[2] + daysOfWeek[3] + daysOfWeek[4]
+ daysOfWeek[5] + daysOfWeek[6]) /7;
}
public String getHotestDay()
{
double min, max;
min = max = daysOfWeek[0];
String day;
day = "";
for (int i = 0; i < daysOfWeek.length; i++)
{
if(min > daysOfWeek[i]) {day = nameOfDays[i]; min = daysOfWeek[i];}
}
return day;
}
public String getCoolestDay()
{
double max = 0;
String day = "";
for (int i = 0; i < daysOfWeek.length; i++)
{
if(max < daysOfWeek[i]) {day = nameOfDays[i]; max = daysOfWeek[i];}
}
return day;
}
public String toString()
{
DecimalFormat df = new DecimalFormat("##.##");
return "Mon Tue Wen Thur Fri Sat Sun\n" + daysOfWeek[0] + " \t" + daysOfWeek[1]
+ " \t" + daysOfWeek[2] + " \t" + daysOfWeek[3] + " \t" + daysOfWeek[4] +
" \t" + daysOfWeek[5] + " \t" + daysOfWeek[6] + "\n\nAverage Temperature
= " + df.format(getAvgTemp()) + "\n\n" + "Hottest day = " + getHotestDay()
+ "\n\n" + "Coolest day = " + getCoolestDay() + "\n";
}
public static void main(String args[])
{
Lab2Ex1 temp = new Lab2Ex1 (12.5, 16.3, 25.5, 30.2, 19.5, 29.5, 10.5);
JOptionPane.showMessageDialog(null, temp.toString());
System.exit(0);
}
}
Re: Beginner: need help
i have made few changes just compare it with your code and i think you can
understand it..
import javax.swing.* ;
import java.text.DecimalFormat ;
public class Lab2Ex1
{
double daysOfWeek[] = new double[7] ;
String nameOfDays[] =
{
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"} ;
public Lab2Ex1 (double inSun, double inMon, double inTues, double inWed,
double inThur, double inFri, double inSat)
{
daysOfWeek[0] = inSun ;
daysOfWeek[1] = inMon ;
daysOfWeek[2] = inTues ;
daysOfWeek[3] = inWed ;
daysOfWeek[4] = inThur ;
daysOfWeek[5] = inFri ;
daysOfWeek[6] = inSat ;
}
public double getAvgTemp ()
{
return (daysOfWeek[0] + daysOfWeek[1] + daysOfWeek[2] +
daysOfWeek[3] +
daysOfWeek[4]
+ daysOfWeek[5] + daysOfWeek[6]) / 7 ;
}
public String getCoolestDay ()
{
double min ;
min = daysOfWeek[0] ;
String day = nameOfDays[0] ;
for (int i = 1 ; i < daysOfWeek.length ; i++)
{
if (min > daysOfWeek[i])
{
day = nameOfDays[i] ;
min = daysOfWeek[i] ;
}
}
return day ;
}
public String getHotestDay ()
{
double max = daysOfWeek[0] ;
String day = nameOfDays[0] ;
for (int i = 1 ; i < daysOfWeek.length ; i++)
{
if (max < daysOfWeek[i])
{
day = nameOfDays[i] ;
max = daysOfWeek[i] ;
}
}
return day ;
}
public String toString ()
{
DecimalFormat df = new DecimalFormat("##.##") ;
return "Mon Tue Wen Thur Fri Sat Sun\n" + daysOfWeek[0] + " \t" +
daysOfWeek[1]
+ " \t" + daysOfWeek[2] + " \t" + daysOfWeek[3] + " \t" +
daysOfWeek[4] +
" \t" + daysOfWeek[5] + " \t" + daysOfWeek[6] +
"\n\nAverage Temperature" +
"= " + df.format(getAvgTemp()) + "\n\n " + "Hottest day = " +
getHotestDay()
+ "\n\n" + "Coolest day = " + getCoolestDay() + "\n" ;
}
public static void main (String args[])
{
Lab2Ex1 temp = new Lab2Ex1(12.5, 16.3, 25.5, 30.2, 19.5, 29.5, 10.5)
;
JOptionPane.showMessageDialog(null, temp.toString()) ;
System.exit(0) ;
}
}
--
Regards
Karthikeyan.R
"Armand" <hoxha301@rogers.com> wrote in message
news:3f2ffdb4$1@tnews.web.devx.com...
>
> I am a beginner in Java.
> I wrote this code to get temperature of the days of the week, get the
coolest,
> hotest and average. Everything looks fine, but when I run it I get wrong
> values for the hottest and coolest day.
> Can anybody tell me what is wrong?
> Please!!!
>
>
>
> import javax.swing.*;
> import java.text.DecimalFormat;
>
> public class Lab2Ex1
> {
> double daysOfWeek[] = new double[7];
> String nameOfDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday",
> "Friday", "Saturday"};
>
> public Lab2Ex1 (double inSun, double inMon, double inTues, double inWed,
> double inThur, double inFri, double inSat)
> {
> daysOfWeek[0] = inSun;
> daysOfWeek[1] = inMon;
> daysOfWeek[2] = inTues;
> daysOfWeek[3] = inWed;
> daysOfWeek[4] = inThur;
> daysOfWeek[5] = inFri;
> daysOfWeek[6] = inSat;
> }
> public double getAvgTemp()
> {
> return (daysOfWeek[0] + daysOfWeek[1] + daysOfWeek[2] + daysOfWeek[3] +
daysOfWeek[4]
> + daysOfWeek[5] + daysOfWeek[6]) /7;
> }
> public String getHotestDay()
> {
> double min, max;
> min = max = daysOfWeek[0];
> String day;
> day = "";
>
> for (int i = 0; i < daysOfWeek.length; i++)
> {
> if(min > daysOfWeek[i]) {day = nameOfDays[i]; min = daysOfWeek[i];}
> }
> return day;
> }
> public String getCoolestDay()
> {
> double max = 0;
> String day = "";
> for (int i = 0; i < daysOfWeek.length; i++)
> {
> if(max < daysOfWeek[i]) {day = nameOfDays[i]; max = daysOfWeek[i];}
> }
> return day;
> }
> public String toString()
> {
> DecimalFormat df = new DecimalFormat("##.##");
> return "Mon Tue Wen Thur Fri Sat Sun\n" + daysOfWeek[0] + " \t" +
daysOfWeek[1]
> + " \t" + daysOfWeek[2] + " \t" + daysOfWeek[3] + " \t" + daysOfWeek[4] +
> " \t" + daysOfWeek[5] + " \t" + daysOfWeek[6] + "\n\nAverage Temperature
> = " + df.format(getAvgTemp()) + "\n\n" + "Hottest day = " + getHotestDay()
> + "\n\n" + "Coolest day = " + getCoolestDay() + "\n";
> }
> public static void main(String args[])
> {
> Lab2Ex1 temp = new Lab2Ex1 (12.5, 16.3, 25.5, 30.2, 19.5, 29.5, 10.5);
>
> JOptionPane.showMessageDialog(null, temp.toString());
> System.exit(0);
> }
> }
Re: Beginner: need help
Thank you for your response.
I'm sorry but I cannot figure out the changes and anyway when I run it still
get the same error.
"Karthikeyan" <rkarthi@comptechsolutions.com> wrote:
>i have made few changes just compare it with your code and i think you can
>understand it..
>
>import javax.swing.* ;
>import java.text.DecimalFormat ;
>
>public class Lab2Ex1
>{
> double daysOfWeek[] = new double[7] ;
> String nameOfDays[] =
> {
> "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
> "Friday", "Saturday"} ;
>
> public Lab2Ex1 (double inSun, double inMon, double inTues, double inWed,
> double inThur, double inFri, double inSat)
> {
> daysOfWeek[0] = inSun ;
> daysOfWeek[1] = inMon ;
> daysOfWeek[2] = inTues ;
> daysOfWeek[3] = inWed ;
> daysOfWeek[4] = inThur ;
> daysOfWeek[5] = inFri ;
> daysOfWeek[6] = inSat ;
> }
>
> public double getAvgTemp ()
> {
> return (daysOfWeek[0] + daysOfWeek[1] + daysOfWeek[2] +
>daysOfWeek[3] +
> daysOfWeek[4]
> + daysOfWeek[5] + daysOfWeek[6]) / 7 ;
> }
>
> public String getCoolestDay ()
> {
> double min ;
> min = daysOfWeek[0] ;
> String day = nameOfDays[0] ;
>
> for (int i = 1 ; i < daysOfWeek.length ; i++)
> {
> if (min > daysOfWeek[i])
> {
> day = nameOfDays[i] ;
> min = daysOfWeek[i] ;
> }
> }
> return day ;
> }
>
> public String getHotestDay ()
> {
> double max = daysOfWeek[0] ;
> String day = nameOfDays[0] ;
> for (int i = 1 ; i < daysOfWeek.length ; i++)
> {
> if (max < daysOfWeek[i])
> {
> day = nameOfDays[i] ;
> max = daysOfWeek[i] ;
> }
> }
> return day ;
> }
>
> public String toString ()
> {
> DecimalFormat df = new DecimalFormat("##.##") ;
> return "Mon Tue Wen Thur Fri Sat Sun\n" + daysOfWeek[0] + " \t"
+
> daysOfWeek[1]
> + " \t" + daysOfWeek[2] + " \t" + daysOfWeek[3] + " \t" +
> daysOfWeek[4] +
> " \t" + daysOfWeek[5] + " \t" + daysOfWeek[6] +
> "\n\nAverage Temperature" +
> "= " + df.format(getAvgTemp()) + "\n\n " + "Hottest day = "
+
> getHotestDay()
> + "\n\n" + "Coolest day = " + getCoolestDay() + "\n" ;
> }
>
> public static void main (String args[])
> {
> Lab2Ex1 temp = new Lab2Ex1(12.5, 16.3, 25.5, 30.2, 19.5, 29.5, 10.5)
>;
>
> JOptionPane.showMessageDialog(null, temp.toString()) ;
> System.exit(0) ;
> }
>}
>
>
>--
>Regards
>Karthikeyan.R
>"Armand" <hoxha301@rogers.com> wrote in message
>news:3f2ffdb4$1@tnews.web.devx.com...
>>
>> I am a beginner in Java.
>> I wrote this code to get temperature of the days of the week, get the
>coolest,
>> hotest and average. Everything looks fine, but when I run it I get wrong
>> values for the hottest and coolest day.
>> Can anybody tell me what is wrong?
>> Please!!!
>>
>>
>>
>> import javax.swing.*;
>> import java.text.DecimalFormat;
>>
>> public class Lab2Ex1
>> {
>> double daysOfWeek[] = new double[7];
>> String nameOfDays[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
>"Thursday",
>> "Friday", "Saturday"};
>>
>> public Lab2Ex1 (double inSun, double inMon, double inTues, double inWed,
>> double inThur, double inFri, double inSat)
>> {
>> daysOfWeek[0] = inSun;
>> daysOfWeek[1] = inMon;
>> daysOfWeek[2] = inTues;
>> daysOfWeek[3] = inWed;
>> daysOfWeek[4] = inThur;
>> daysOfWeek[5] = inFri;
>> daysOfWeek[6] = inSat;
>> }
>> public double getAvgTemp()
>> {
>> return (daysOfWeek[0] + daysOfWeek[1] + daysOfWeek[2] + daysOfWeek[3]
+
>daysOfWeek[4]
>> + daysOfWeek[5] + daysOfWeek[6]) /7;
>> }
>> public String getHotestDay()
>> {
>> double min, max;
>> min = max = daysOfWeek[0];
>> String day;
>> day = "";
>>
>> for (int i = 0; i < daysOfWeek.length; i++)
>> {
>> if(min > daysOfWeek[i]) {day = nameOfDays[i]; min = daysOfWeek[i];}
>> }
>> return day;
>> }
>> public String getCoolestDay()
>> {
>> double max = 0;
>> String day = "";
>> for (int i = 0; i < daysOfWeek.length; i++)
>> {
>> if(max < daysOfWeek[i]) {day = nameOfDays[i]; max = daysOfWeek[i];}
>> }
>> return day;
>> }
>> public String toString()
>> {
>> DecimalFormat df = new DecimalFormat("##.##");
>> return "Mon Tue Wen Thur Fri Sat Sun\n" + daysOfWeek[0] + " \t" +
>daysOfWeek[1]
>> + " \t" + daysOfWeek[2] + " \t" + daysOfWeek[3] + " \t" + daysOfWeek[4]
+
>> " \t" + daysOfWeek[5] + " \t" + daysOfWeek[6] + "\n\nAverage Temperature
>> = " + df.format(getAvgTemp()) + "\n\n" + "Hottest day = " + getHotestDay()
>> + "\n\n" + "Coolest day = " + getCoolestDay() + "\n";
>> }
>> public static void main(String args[])
>> {
>> Lab2Ex1 temp = new Lab2Ex1 (12.5, 16.3, 25.5, 30.2, 19.5, 29.5, 10.5);
>>
>> JOptionPane.showMessageDialog(null, temp.toString());
>> System.exit(0);
>> }
>> }
>
>