average scores/ due today/ 2 errors has to do with the panel set up

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1671 Views - Last Post: 29 July 2011 - 04:58 PM Rate Topic: -----

#31 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:04 PM

Very cool, I can't believe how much I learn from this site...Sometimes it only takes a 2x4 :)
1 error for the minute =

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: variable totalGrades might not have been initialized
topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));
^
1 error

Tool completed with exit code 1

/*
Name: 		Charyl Janney
Date:		July 21 2011
Project: 	Chpt5ex3
File Name:	AverageScores.java
This application calculates an average upon enter number of
scores and calculating an average of these scores.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;


public class AverageScores extends Frame
{

//Declare variable
	  Panel topPanel;
	  int nrOfTests[];
	  double sumOfAllGrades;


public AverageScores()
{
		 double totalGrades;

		topPanel = new Panel();
		//setup frame

		this.setLayout(new BorderLayout());
		topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));

		//add components to frame
	    add(topPanel,BorderLayout.NORTH);
	}

 public static void main(String[] args) throws IOException
{


	try
    {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );

	 System.out.print( "Please enter number of tests: " );

         int nrOfTests = Integer.parseInt(input.readLine());

         //the sumOfAllGrades
         double sumOfAllGrades = 0;

         for( int i=0; i < nrOfTests; i++ )

    {

         System.out.print( "Enter the grade for Test " +(i+1)+ ": " );

         double grade = Double.parseDouble(input.readLine());

	 //add current grade to sumOffAllGrades
          sumOfAllGrades += grade;
    }
	 //calculating average
         double average = sumOfAllGrades/nrOfTests;
         System.out.println("The total grade average is: " + average);

     }

         catch( Exception e )

     {

           e.printStackTrace();
     }
     //sets Frame up
     		 AverageScores f = new AverageScores();
			 f.setTitle("Average Scores");
			 f.setBounds(50, 100, 300, 400);
			 f.setLocationRelativeTo(null);
             f.setVisible(true);
             {
		}

  }

}



Was This Post Helpful? 0
  • +
  • -

#32 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:05 PM

You declare it here double totalGrades;, but before you can use it you must initialize it, or assign it a default value like 0.
Was This Post Helpful? 0
  • +
  • -

#33 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:12 PM

sorry, I was messing with a few things just to see forgot I had changed my code;

/*
Name: 		Charyl Janney
Date:		July 21 2011
Project: 	Chpt5ex3
File Name:	AverageScores.java
This application calculates an average upon enter number of
scores and calculating an average of these scores.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;


public class AverageScores extends Frame
{

//Declare variable
	  Panel topPanel;



public AverageScores()
{
		 double totalGrades;

		topPanel = new Panel();
		//setup frame

		this.setLayout(new BorderLayout());
		topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));

		//add components to frame
	    add(topPanel,BorderLayout.NORTH);
	}

 public static void main(String[] args) throws IOException
{


	try
    {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );

	 System.out.print( "Please enter number of tests: " );

         int nrOfTests = Integer.parseInt(input.readLine());

         //the sumOfAllGrades
         double sumOfAllGrades = 0;

         for( int i=0; i < nrOfTests; i++ )

    {

         System.out.print( "Enter the grade for Test " +(i+1)+ ": " );

         double grade = Double.parseDouble(input.readLine());

	 //add current grade to sumOffAllGrades
          sumOfAllGrades += grade;
    }
	 //calculating average
         double average = sumOfAllGrades/nrOfTests;
         System.out.println("The total grade average is: " + average);

     }

         catch( Exception e )

     {

           e.printStackTrace();
     }
     //sets Frame up
     		 AverageScores f = new AverageScores();
			 f.setTitle("Average Scores");
			 f.setBounds(50, 100, 300, 400);
			 f.setLocationRelativeTo(null);
             f.setVisible(true);
             {
		}

  }

}




I'm inbetween studying for a test at 5pm, replying to another online class that's also due and this as well. Last day of school, have to multi task. Think I have 3 new gray hairs.
Was This Post Helpful? 0
  • +
  • -

#34 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:12 PM

So did you get your problem worked out? Or is there another issue?
Was This Post Helpful? 0
  • +
  • -

#35 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:47 PM

okay, Yeah,,,,sort of. it compiles :)!!!But I forgot to put in the code to change the background color. Had to change the double to int. I'll be back. You are soooo smart and I learned frame != Frame. plus a bunch more. int = 0 be back momentarily have another question on a different program.
Was This Post Helpful? 0
  • +
  • -

#36 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 01:48 PM

Glad I've been able to help! :)
Was This Post Helpful? 0
  • +
  • -

#37 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 02:11 PM

okay, teach me one more thing. I swiped this code off one of my other programs I compiled but it appearantly doesn't work the same way here.

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:96: class, interface, or enum expected
}
^
1 error

Tool completed with exit code 1


/*
Name: 		Charyl Janney
Date:		July 21 2011
Project: 	Chpt5ex3
File Name:	AverageScores.java
This application calculates an average upon enter number of
scores and calculating an average of these scores.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;


public class AverageScores extends Frame
{

//Declare variable
	  Panel topPanel;
		int totalGrades;


public AverageScores()
{
		int totalGrades = 0;
		topPanel = new Panel();

		//setup frame

		this.setLayout(new BorderLayout());
		topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));

		//add components to frame
	    add(topPanel,BorderLayout.NORTH);
	}

 public static void main(String[] args) throws IOException
{


	try
    {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );

	 System.out.print( "Please enter number of tests: " );

         int nrOfTests = Integer.parseInt(input.readLine());

         //the sumOfAllGrades
         double sumOfAllGrades = 0;

         for( int i=0; i < nrOfTests; i++ )

    {

         System.out.print( "Enter the grade for Test " +(i+1)+ ": " );

         double grade = Double.parseDouble(input.readLine());

	 //add current grade to sumOffAllGrades
          sumOfAllGrades += grade;
    }
	 //calculating average
         double average = sumOfAllGrades/nrOfTests;
         System.out.println("The total grade average is: " + average);

     }

         catch( Exception e )

     {

           e.printStackTrace();
     }
     //sets Frame up
     		 AverageScores f = new AverageScores();
			 f.setTitle("Average Scores");
			 f.setBounds(50, 100, 300, 400);
			 f.setLocationRelativeTo(null);
             f.setVisible(true);
             {

				            //  topDisplay() = new TextArea(null, 3,5,3);
				            //  topDisplay().setText();
				            //  topDisplay().setEditable(false);
				            //  topPanel.add(topDisplay());
				              topDisplay().setBackground(Color.yellow);
            }
		}

  }

}



Was This Post Helpful? 0
  • +
  • -

#38 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 02:27 PM

You have one too many closing brackets, delete the last one.

With the code you commented out, topDisplay is undefined. What are you trying to do? Did you mean to set the setBackground color on f? If you are changing f, do it before you set it visible.

This post has been edited by GregBrannon: 29 July 2011 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#39 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 02:36 PM

Be back in a bit/ reviewing with class for last test. This one's all from memory, didn't have time to study. Ohhh the pain. Remember when we where young and used to stay up all night because we where afraid we were going to miss something or because we where having soooo much fun :)
Was This Post Helpful? 0
  • +
  • -

#40 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 04:44 PM

ok, I don't know if you're still there,but back to work for me.
Was This Post Helpful? 0
  • +
  • -

#41 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 04:52 PM

ok, I'm too tired to do this right now, I'm turning it in as it was when it compilled correctly and I will come back to learn this another day.
Was This Post Helpful? 0
  • +
  • -

#42 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: average scores/ due today/ 2 errors has to do with the panel set up

Posted 29 July 2011 - 04:58 PM

If not too late, change the last bit to:


        //sets Frame up
        AverageScores f = new AverageScores();
        f.setTitle("Average Scores");
        f.setBounds(50, 100, 300, 400);
        f.setLocationRelativeTo(null);
        f.setBackground( Color.yellow ); // added this line
        f.setVisible(true);



and delete that code you added before leaving.

Is that what you were trying to do?

This post has been edited by GregBrannon: 29 July 2011 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3