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

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

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

#1 charyl  Icon User is offline

  • D.I.C Regular

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

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

Posted 29 July 2011 - 03:09 AM

hey there, I hope someone is up :) My errors revolve around the panel I'm trying to set up. Have finals so I've been up all night. Can't seem to figure it out. Know it's simple, just can't see. my code is this:
/*
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()
{
		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);
             {
		}
  }
}





It runs fine without the border/panels. My errors are this:

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: invalid method declaration; return type required
add(topPanel.BorderLayout.NORTH);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: <identifier> expected
add(topPanel.BorderLayout.NORTH);
^
2 errors

Tool completed with exit code 1
Can anyone help?

Is This A Good Question/Topic? 0
  • +

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

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1376
  • View blog
  • Posts: 3,028
  • Joined: 05-April 11

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

Posted 29 July 2011 - 04:02 AM

add(topPanel.BorderLayout.NORTH);

dont you mean add(topPanel, BorderLayout.NORTH);
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 04:09 AM

Why aren't you using the Swing JPanel?

For your first error, the line

add(topPanel.BorderLayout.NORTH);

exists outside a method, so the compiler doesn't understand what it is. It thinks it's an incorrect method definition.

Edited for Panel rather than JPanel: The second error is pointing to the same line which is calling a method that you're using incorrectly as CasiOo pointed out.

As for the rest of your code . . . come back if you have more problems.

This post has been edited by GregBrannon: 29 July 2011 - 04:13 AM

Was This Post Helpful? 2
  • +
  • -

#4 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:50 AM

I'm too new to understand how to use JPanel. but I did change the add(topPanel, BorderLayout.NORTH); so now I have 3 errors. I need to google to understand a little more. It's all pointing to the Swing Jpanel. I'm just not sure what I'm doing.

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: invalid method declaration; return type required
add(topPanel, BorderLayout.NORTH);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: <identifier> expected
add(topPanel, BorderLayout.NORTH);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: <identifier> expected
add(topPanel, BorderLayout.NORTH);
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 04:57 AM

Did you move the add() statement inside the preceding method, AverageScores()? You have a closing bracket (line 28) out of place.
Was This Post Helpful? 1
  • +
  • -

#6 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 - 05:11 AM

ok, I moved my closing bracket and now I have these errors:

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: illegal start of expression
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: illegal start of expression
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: ';' expected
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: '.class' expected
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: ';' expected
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: illegal start of expression
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: ';' expected
public static void main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:80: reached end of file while parsing
}
^
8 errors

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();


public AverageScores()
{
		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 think I'm slap happy.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 05:15 AM

Please pay attention to what you're doing. You moved the bracket but changed it from a closing bracket to an opening bracket. As pbl likes to remind, proper indention would help you see these errors.
Was This Post Helpful? 1
  • +
  • -

#8 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 - 05:24 AM

ok by taking out the public static void on line 33 I'm now down to 5 errors

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: '.class' expected
main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: ';' expected
main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: illegal start of expression
main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:33: ';' expected
main(String[] args) throws IOException
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:80: reached end of file while parsing
}
^
5 errors

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 05:30 AM

Quote

ok by taking out the public static void . . .


Huh? Why would you do that?
Was This Post Helpful? 1
  • +
  • -

#10 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 - 05:42 AM

I'm in the game! :) Coffee anyone? I'm now at 12 errors and I need to start a 3 hour test. I have another 3 hour test scheduled at 12 and my final test is at 5pm. I know it's not that difficult, but I guess we all have to start somewhere.
What kills me is I've coded frame/panel layouts and the silly code works fine without the border layout, but I'm too determined to get that right just because I've done it successfully. Think I've been at it too long. Started on this at 4:30am.

C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:15: cannot find symbol
symbol: class frame
public class AverageScores extends frame
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:19: missing method body, or declare abstract
Panel topPanel();
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:24: cannot find symbol
symbol : variable topPanel
location: class AverageScores
topPanel = new Panel();
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:26: cannot find symbol
symbol : method setLayout(java.awt.BorderLayout)
location: class AverageScores
this.setLayout(new BorderLayout());
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:27: cannot find symbol
symbol : variable totalGrades
location: class AverageScores
topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:27: cannot find symbol
symbol : constructor GridLayout(<nulltype>,int,int,int)
location: class java.awt.GridLayout
topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:27: cannot find symbol
symbol : variable topPanel
location: class AverageScores
topPanel.setLayout(new GridLayout((totalGrades + 1),2,10,10));
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:30: cannot find symbol
symbol : variable topPanel
location: class AverageScores
add(topPanel,BorderLayout.NORTH);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:73: cannot find symbol
symbol : method setTitle(java.lang.String)
location: class AverageScores
f.setTitle("Average Scores");
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:74: cannot find symbol
symbol : method setBounds(int,int,int,int)
location: class AverageScores
f.setBounds(50, 100, 300, 400);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:75: cannot find symbol
symbol : method setLocationRelativeTo(<nulltype>)
location: class AverageScores
f.setLocationRelativeTo(null);
^
C:\Documents and Settings\CE User\Desktop\Java_OnLine_Course\chpt6\AverageScores.java:76: cannot find symbol
symbol : method setVisible(boolean)
location: class AverageScores
f.setVisible(true);
^
12 errors

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();


public AverageScores()
{
		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);
             {
		}

  }

}




sadly, one of my classmates gave me his code to use, but it's written so eloquently and beautifully that I don't have the heart to use it.
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 06:03 AM

Error 1 and many others: Java is case sensitive, frame != Frame
Error 2 and 3 and ?: Panel is not recognized. Have you imported the necessary references? (Seems so.) This error may be a remnant of another.

Fix especially the first, and recompile. Hopefully, you'll be back with significantly fewer errors.
Was This Post Helpful? 1
  • +
  • -

#12 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 - 06:17 AM

can a driver class can access a private variable directly?
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 29 July 2011 - 06:25 AM

Working on your test now? Open book, internet, and life lines available?

You may be able to answer the question after you review this page.
Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

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

Posted 29 July 2011 - 06:35 AM

Quote

I'm too new to understand how to use JPanel.

Swing is actually easier to use than AWT in some cases, and in most cases you'll hardly notice a difference. There is far more functionality built into Swing for you to use than in AWT. Check out my blog entry on the Java GUI for more on why to use Swing. Also, the fact that you are new shouldn't inhibit you from adhering to standards in this case.
Was This Post Helpful? 1
  • +
  • -

#15 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 - 06:49 AM

first person to make me giggle today. I'm going to say no because it doesn't say the driver class is in with the private, i have to go back and re read. But my guess is it has to be involved directly or nothing outside can touch a private variable.
Was This Post Helpful? 0
  • +
  • -

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