7 Replies - 268 Views - Last Post: 19 May 2014 - 05:59 AM Rate Topic: -----

#1 Syre Lancaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 04-May 14

Trouble with Methods and Arrays (Beginner Level)

Posted 18 May 2014 - 11:00 PM

Hello everyone. I'm having a bit of trouble with a segement of code I am working on. Basically what I am trying to do is get a grading system working for some students that will tell them their name, numeric and letter grade value. I am doing this by using a switch statement to analyze what letter grade they are going to get.

Now, we haven't really gone over arrays yet in class, so I'm kinda at at loss on how to pass the values from the arrays into the methods. I have commented the section below that is throwing me an error.

Now, this is just a rough draft, I am a noob and haven't been able to compile this program yet so if anyone sees any other errors other than the obvious ones, then I would appreciate it if you let me know!

Thanks!



package org.CIS406.lab4;

public class Roster {
	//Declarations
	String[] lastName = {"Byrd", "Mari", "Theoden", "Wallace, Rollins" };
	int[] grades = {93, 85, 77, 65, 53};
	char finalLetter;
	char letterGrade;
	
	//Main Method
	public static void main(String[] args)
	{
			
		//Method calls  >>>>EXCEPTION THROWN HERE<<<< "can not make a static reference to the non-static field" 
		DetermineGrade(lastName,grades);
		DisplayGrades(lastName,grades);
	
	}
	
	
	//Display Grades Method
	public void DisplayGrades(String[] name, int[] grade)
	
	{
		int x; //name iterator
		int y; //grade iterator
		//For loops for displaying the arrays
		for (x=0; x < name.length; x++)
			for (y=0; y< grade.length; y++)
				System.out.println (name[x] + grade[y]);
		DisplayLetter();  //Display the letter grade
	}
	
	//Display Letter Grade Method
	public void DisplayLetter ()
	{
		System.out.println("Letter grade:"+ finalLetter);
	}
	
	//Determine letter grade Method
	public void DetermineGrade (String[] name, int[] grade)
	{
		
		
		switch(letterGrade)
		{
		case 0:
			for (int i= 0; i < grades.length; i++)                  
				if(grades[i] >= 90 && grades[i] <= 100)     
				{	
					//Set letter grade value 
					finalLetter = 'A';
				}
		case 1:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 80 && grades[i] < 90) 
				 {	
					//Set letter grade value 
					 finalLetter = 'B';
				 }
		case 2:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 60 && grades[i] < 70) 
				 	{	
					//Set letter grade value 
					 finalLetter = 'C';
				 	}
		case 3:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 50 && grades[i] < 60) 
				 	{	
					//Set letter grade value 
					 finalLetter = 'D';
				 	}
		case 4:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] < 50) 
				 	{	
					//Set letter grade value  
					 finalLetter  = 'F';
				 	}
		}
		
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with Methods and Arrays (Beginner Level)

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 18 May 2014 - 11:07 PM

Quote

//Method calls >>>>EXCEPTION THROWN HERE<<<< "can not make a static reference to the non-static field"

This error message (note this is not an Exception, this is a syntax error) is very self-explanatory. You can only invoke static methods within a static method. So declare the other methods as static.

As you grow as a developer and learn about OOP, you will learn more standards adherent ways of designing your program. For now, declaring your methods as static is the easiest way to fix the problem.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7645
  • View blog
  • Posts: 12,898
  • Joined: 19-March 11

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 18 May 2014 - 11:07 PM

You're getting this exception because the main() method runs as a static method, and your Determine and Display methods are instance methods.
The difference is, the static method runs without a particular object as its context, and the instance method has exactly one instance as its context. The instance method then can know about the particular state of the particular object that it is bound to - obviously, since a static method is not bound to an instance, it can't know about any instance's context.

The correct way to fix this is to create an instance of your Roster class and then call that object's methods.


something like

Roster roster = new Roster();
roster.DetermineGrades();
roster.DisplayGrades();



should do the trick. Do some reading on static and instance in java and see what you can learn about this, it's worth knowing.

Also, in Java methods should be in initialLowerCase.
Was This Post Helpful? 0
  • +
  • -

#4 Syre Lancaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 04-May 14

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 19 May 2014 - 01:38 AM

Thank you both, those are very helpful tips. For now I changed everything to static and its compiling now, but the letter grade output is blank. And I'm kinda stumped on this now. Anyone have any clues for me?

package org.CIS406.lab4;

public class Roster {
	//Declarations
	static String[] lastName = {"Byrd", "Mari", "Theoden", "Wallace", "Rollins" };
	static int[] grades = {93, 85, 77, 65, 53};
	static char finalLetter;
	static char letterGrade;
	
	//Main Method
	public static void main(String[] args)
	{
			
		//Method calls
		DetermineGrade(lastName,grades);
		DisplayGrades(lastName,grades);
	
	}
	
	
	//Display Grades Method
	public static void DisplayGrades(String[] name, int[] grade)
	
	{
	
		 
		//For loops for displaying the arrays
		for (int x=0; x < name.length; x++)
			System.out.println("Name:" + name[x]);
		
		for (int y=0; y< grade.length; y++)
			System.out.println ("Grade:" + grade[y]);
		
		System.out.println("Letter grade:"+ finalLetter); //Test to see if finalLetter is passing back
		
		DisplayLetter();  //Display the letter grade
	}
	
	//Determine letter grade Method
	public static void DetermineGrade (String[] name, int[] grade)
	{
		int y= 0;
		while (y < grade.length)
		{
		switch(letterGrade)
		{
		case 'A':
			for (int i= 0; i < grades.length; i++)                  
				if(grades[i] > 90 && grades[i] < 100)     
				{	
					//Set letter grade value 
					finalLetter = 'A';
					break;
					
				}
		case 'B':
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 80 && grades[i] < 90) 
				 {	
					//Set letter grade value 
					 finalLetter = 'B';
					 break;
				 }
		case 'C':
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 60 && grades[i] < 70) 
				 	{	
					//Set letter grade value 
					 finalLetter = 'C';
					 break;
				 	}
		case 'D':
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 50 && grades[i] < 60) 
				 	{	
					//Set letter grade value 
					 finalLetter = 'D';
					 break;
				 	}
		case 'F':
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] < 50) 
				 	{	
					//Set letter grade value  
					 finalLetter  = 'F';
					 break;
				 	}
					 
		default:
			System.out.println("Invalid Entry");
			y++;
					 
				 	
		}
		}
		}
	//Display Letter Grade Method
	public static void DisplayLetter ()
		{
			System.out.println("Letter grade:"+ finalLetter);
		}
		
	}
	




Was This Post Helpful? 0
  • +
  • -

#5 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 159
  • View blog
  • Posts: 623
  • Joined: 24-April 10

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 19 May 2014 - 03:10 AM

your switch is using a letterGrade variable which is not assigned to anything when the switch starts. Actually letterGrade is never assigned anything throught the entire program. Since you are using a number grade to determine a letter grade, maybe an if-else-if is the way to go?

This post has been edited by mike73: 19 May 2014 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 Syre Lancaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 04-May 14

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 19 May 2014 - 03:41 AM

Nah, it called for specifically switch statements to determine the letter grade, which IMO is kinda stupid since we haven't gone over alot of this stuff yet and if else would certainly make this far easier.

They pretty much threw us to the wolves this week >.>


I changed a few things around, and got the program to start returning "D" for all the letter grades. Not sure how exactly I did that, but it's something.

//Determine letter grade Method
	public static String DetermineGrade (String[] name, int[] grade)
	{
		int y= 0;
		while (y < grade.length)
		{
		switch(letterGrade)
		{
		case 0:
			for (int i= 0; i < grades.length; i++)                  
				if(grades[i] >= 90 && grades[i] < 100)     
				{	
					//Set letter grade value 
					finalLetter = "A";
					break;
					
				}
		case 1:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 80 && grades[i] < 90) 
				 {	
					//Set letter grade value 
					 finalLetter = "B";
					 break;
				 }
		case 2:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 60 && grades[i] < 70) 
				 	{	
					//Set letter grade value 
					 finalLetter = "C";
					 break;
				 	}
		case 3:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] >= 50 && grades[i] < 60) 
				 	{	
					//Set letter grade value 
					 finalLetter = "D";
					 break;
				 	}
		case 4:
			for (int i= 0; i < grades.length; i++)
				 if(grades[i] < 50) 
				 	{	
					//Set letter grade value  
					 finalLetter  = "F";
					 break;
				 	}
					 
		default:
			System.out.println("Invalid Entry");
			
			
			
				 	
		}
		}
		return finalLetter;
		}


Was This Post Helpful? 0
  • +
  • -

#7 Zavael  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-May 14

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 19 May 2014 - 05:28 AM

you should try to output more information to see what is happening, do not fear to put it where you do not know what case was selected or if the code is going through some "if's", for example
if(grades[i] >= 80 && grades[i] < 90)
{ 
       //Set letter grade value
       System.out.println("grade is between 80 and 90: " + grades[i]);
       finalLetter = "B";
       break;
}



on the other hand, i think you are checking for all the grades for every person, to me it seems you should match the grades to the users i.e. lastName[1] to grades [1], lastName[2] to grades[2]...
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7645
  • View blog
  • Posts: 12,898
  • Joined: 19-March 11

Re: Trouble with Methods and Arrays (Beginner Level)

Posted 19 May 2014 - 05:59 AM

		int y= 0;
		while (y < grade.length)


Use a for loop for this instead - what you're doing is equivalent in terms of the generated jvm code, but the for loop construction expresses what you're actually trying to do. The rule is: use a for when you're doing a loop over a known range, use a while when you're looping until a certain condition changes.


		switch(letterGrade)


It looks to me like you're trying to set the letter grade. In that case, you'd switch on the percentage - that is, on grades[i]


			for (int i= 0; i < grades.length; i++)                  
				if(grades[i] >= 90 && grades[i] < 100)     
				{	
					//Set letter grade value 
					finalLetter = "A";
					break;
				}


This will enter the loop, cycle through the grades array, and if it finds a grade in this range it sets the finalLetter to "A" and then exits the for loop. If it does not find a value in that range, it doesn't do anything. It then falls through to the next case (because there is no break on the case, only on the for loop) and does the same thing again for each case. The only thing you get from putting this in a switch is that the switch value determines where you start - that is, if your switch value is 2, then the maximum possible finalLetter is "C" (but it'll be lower if any grade in the array is less than 60)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1