I need to create more methods

It is a student grade program...

Page 1 of 1

7 Replies - 547 Views - Last Post: 22 September 2009 - 05:15 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

I need to create more methods

Posted 22 September 2009 - 02:25 PM

I wrote this program, but when I went back to the directions from my prof, I didn't write it correctly...I don't think so at least. I was wondering if I could get some help on writing accroding to my profs directions. The program functions properly and all is well, just need a few tweeks. The assignment is as follows: Write a class encapsulating the concept of student grades on a test, assuming student grades are composed of a list of integres between 0 and 100. Write the following methods: 1) a constructor with just one parameter, the number of students; all grades can be randomly generated, 2) accessor, mutator, and toString methods, 3) a method returning an array of the grades sorted in ascending order, 4) a method returning the highest grade, 5) a method returning the average grade. Write a client class to test all the methods in your class. AND the chance for some extra credit if I can is....
Extra credit: Write two additional methods for the StudentGrades class--one method for returning the median grade and one method for returning the mode.

This is what I have for now....

import java.util.*;
import java.io.*;

public class STUDENT_GRADES
{
	public static void main(String[] args)
	{
		double avg;
		int a;
		int totStudents = 15;
		int highestGrade = 100;
		int grades[] = new int[highestGrade]; 
		
		for (int i = 0; i <= totStudents; i++)
		{
			grades[i] = (int)(Math.random() * 100) + 1;
		}
		
		for (int i = 1; i <= totStudents; i++)
		{
			System.out.println("Student number " + i + "'s grade is: " + grades[i]);
			
		}
		
		System.out.println("\nThe grades for the 15 students are as follows:");

	bubbleSort(grades, 15);
		for (a = 0; a < 15; a++)
			System.out.print(grades[a] + " ");
		
		System.out.println("");
		System.out.print("\nThe highest grade in the class is: " + grades[14]);
		System.out.print("%");
		System.out.println("");

	avg = ((grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grades[5] +	grades[6] + grades[7] +
					grades[8] + grades[9] + grades[10] + grades[11] + grades[12] + grades[13] + grades[14]) /	totStudents);	
	
	System.out.printf("The average of the class is: %.2f", avg);
	System.out.print("%");
	System.out.println("");

	}
		//Method bubbleSort
	public static void bubbleSort(int grades[], int gradesLength)
	{
		//Initializing and declaring variables
		int fNow;
		int count;
		int numbers;
		
		//Starting loop to sort
		for (count = 0; count < gradesLength; count++)
		{
			//Starting loop inside of sort loop for total list
			for (numbers = 0; numbers < gradesLength - 1; numbers++)
			
				//Declaring how list is formatted
				if (grades[numbers] > grades[numbers + 1])
				{
					//Declaring new order of list
					fNow = grades[numbers];
					grades[numbers] = grades[numbers + 1];
					grades[numbers + 1] = fNow;
				}
		}
	}
	
	STUDENT_GRADES()
	{
		int totStudents = 15;
	}
}



:D I would appreciate any help!!!! :)

Is This A Good Question/Topic? 0
  • +

Replies To: I need to create more methods

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: I need to create more methods

Posted 22 September 2009 - 03:06 PM

the simplest and first thing you should do is add 2 instance variables, one is the number of students and the other is the array of grades. 2nd thing is fix your constructor to fit the description and add a parameter to it which represents the number of students. when the constructor is called the array of grades should be created and have its values set by a random number generator. The rest of the methods should be easy to implement
Was This Post Helpful? 0
  • +
  • -

#3 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: I need to create more methods

Posted 22 September 2009 - 03:09 PM

View Postmostyfriedman, on 22 Sep, 2009 - 02:06 PM, said:

the simplest and first thing you should do is add 2 instance variables, one is the number of students and the other is the array of grades. 2nd thing is fix your constructor to fit the description and add a parameter to it which represents the number of students. when the constructor is called the array of grades should be created and have its values set by a random number generator. The rest of the methods should be easy to implement


Can you show me some of the lines I need to fix, and their fixes...I'm not sure on what you are saying. I thought I had a constructor for the number of students at the bottom...
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: I need to create more methods

Posted 22 September 2009 - 03:18 PM

yes but your constructor has no parameter, the description says that your constructor must have one parameter
import java.util.Random;
public class StudentGrades
{
	double [] grades;
	static Random random = new Random();
	public StudentGrades(int n)
	{
		  grades = new double[n];
		  for(int i = 0; i < grades.length; i++)
		  {
				 grades[i] = random.nextDouble()*100;
		  }
	}
}



there you go, the constructor and the instance variables that you would need. the rest of the methods are pretty simple, give it a shot ;)

This post has been edited by mostyfriedman: 22 September 2009 - 03:19 PM

Was This Post Helpful? 1
  • +
  • -

#5 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: I need to create more methods

Posted 22 September 2009 - 04:30 PM

It must just be me...I cannot get it to work. No matter what I do, I get a compiler error that I can't call STUDENT_GRADES, I am lost. This is what I have now, it works no problems, but I need help with the methods and the calling of those methods.


import java.util.*;
import java.io.*;

public class STUDENT_GRADES
{
	double [] grades;
	static Random random = new Random();

	public static void main(String[] args)
	{
		double avg;
		int a;
		int totStudents = 15;
		int highestGrade = 100;
		int grades[] = new int[highestGrade]; 

		
		for (int i = 0; i <= totStudents; i++)
		{
			grades[i] = (int)(Math.random() * 100) + 1;
		}
		
		for (int i = 1; i <= totStudents; i++)
		{
			System.out.println("Student number " + i + "'s grade is: " + grades[i]);
			
		}
		
		System.out.println("\nThe grades for the 15 students are as follows:");

	bubbleSort(grades, 15);
		for (a = 0; a < 15; a++)
			System.out.print(grades[a] + " ");
		
		System.out.println("");
		System.out.print("\nThe highest grade in the class is: " + grades[14]);
		System.out.print("%");
		System.out.println("");

	avg = ((grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grades[5] +	grades[6] + grades[7] +
					grades[8] + grades[9] + grades[10] + grades[11] + grades[12] + grades[13] + grades[14]) /	totStudents);	
	
	System.out.printf("The average of the class is: %.2f", avg);
	System.out.print("%");
	System.out.println("");
	
	System.out.println("The median in the class is: " + grades[7]);
	
	}
		//Method bubbleSort
	public static void bubbleSort(int grades[], int gradesLength)
	{
		//Initializing and declaring variables
		int fNow;
		int count;
		int numbers;
		
		//Starting loop to sort
		for (count = 0; count < gradesLength; count++)
		{
			//Starting loop inside of sort loop for total list
			for (numbers = 0; numbers < gradesLength - 1; numbers++)
			
				//Declaring how list is formatted
				if (grades[numbers] > grades[numbers + 1])
				{
					//Declaring new order of list
					fNow = grades[numbers];
					grades[numbers] = grades[numbers + 1];
					grades[numbers + 1] = fNow;
				}
		}
	}
	
	public STUDENT_GRADES(int n)
	{
	//	int totStudents = 15;
		
		grades = new double[n];
			
			for(int i = 0; i < grades.length; i++)
			{
				grades[i] = random.nextDouble()*100;
			}
	}
}


I just need further explanation on what I need to do, not to say that I want the exact code, but explanations of what is going on and how it works...
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: I need to create more methods

Posted 22 September 2009 - 04:55 PM

you need to take some stuff out your main method and provide appropriate instance methods to do it just to follow the description and also you had some mistakes in your bubble sort which i fixed. now instead of printing the array, you could provide a toString method for your Student grades object that you create which will print out the grades. I have provided methods with empty bodies for you that you will need to implement to help you out a little. i hope you understand the whole point of encapsulation. you would want to write reusable pieces of code for the user to use instead of having to rewrite everything from scratch each time. this is done by writing appropriate methods associated with the object you are creating so when someone wants to use your Student grades class he wouldnt have to write out a method to do the basic stuff like printing the grades or finding the avg etc. hope this helps a little

import java.util.*;
import java.io.*;

public class STUDENT_GRADES {
	double[] grades;

	static Random random = new Random();

	public static void main(String[] args) {
	double avg;
	int a;
	int totStudents = 15;
	int highestGrade = 100;

	STUDENT_GRADES grades = new STUDENT_GRADES(totStudents);//created an instance of the class

	System.out.println("\nThe grades for the 15 students are as follows:");

	grades.bubbleSort(); // this is how you'd call the method to sort the grades

	System.out.println("");
	System.out.print("\nThe highest grade in the class is: " + grades[14]);
	System.out.print("%");
	System.out.println("");

	System.out.printf("The average of the class is: %.2f", avg);
	System.out.print("%");
	System.out.println("");

	System.out.println("The median in the class is: " + grades[7]);

	}

	// Method bubbleSort
	public void bubbleSort() {
	// Initializing and declaring variables
	double fNow;
	int count;
	int numbers;

	// Starting loop to sort
	for (count = 0; count < grades.length - 1; count++) {
		// Starting loop inside of sort loop for total list
		for (numbers = 0; numbers < grades.length - 1 - count; numbers++)

		// Declaring how list is formatted
		if (grades[numbers] > grades[numbers + 1]) {
			// Declaring new order of list
			fNow = grades[numbers];
			grades[numbers] = grades[numbers + 1];
			grades[numbers + 1] = fNow;
		}
	}
	}

	public STUDENT_GRADES(int n) {
	// int totStudents = 15;

	grades = new double[n];

	for (int i = 0; i < grades.length; i++) {
		grades[i] = random.nextDouble() * 100;
	}
	}

	public String toString() {
	String output = "";
	for (int i = 0; i < grades.length; i++) {
		output += "Student number " + (i + 1) + "'s grade is: " + grades[i]
			+ "\n";

	}
	return output;
	}

	public double average() {
	// write the code to calculate the average here
	}

	public double highestGrade() {
	// code to find the highest value
	}
}


This post has been edited by mostyfriedman: 22 September 2009 - 04:58 PM

Was This Post Helpful? 1
  • +
  • -

#7 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: I need to create more methods

Posted 22 September 2009 - 05:11 PM

View Postmostyfriedman, on 22 Sep, 2009 - 03:55 PM, said:

you need to take some stuff out your main method and provide appropriate instance methods to do it just to follow the description and also you had some mistakes in your bubble sort which i fixed. now instead of printing the array, you could provide a toString method for your Student grades object that you create which will print out the grades. I have provided methods with empty bodies for you that you will need to implement to help you out a little. i hope you understand the whole point of encapsulation. you would want to write reusable pieces of code for the user to use instead of having to rewrite everything from scratch each time. this is done by writing appropriate methods associated with the object you are creating so when someone wants to use your Student grades class he wouldnt have to write out a method to do the basic stuff like printing the grades or finding the avg etc. hope this helps a little

import java.util.*;
import java.io.*;

public class STUDENT_GRADES {
	double[] grades;

	static Random random = new Random();

	public static void main(String[] args) {
	double avg;
	int a;
	int totStudents = 15;
	int highestGrade = 100;

	STUDENT_GRADES grades = new STUDENT_GRADES(totStudents);//created an instance of the class

	System.out.println("\nThe grades for the 15 students are as follows:");

	grades.bubbleSort(); // this is how you'd call the method to sort the grades

	System.out.println("");
	System.out.print("\nThe highest grade in the class is: " + grades[14]);
	System.out.print("%");
	System.out.println("");

	System.out.printf("The average of the class is: %.2f", avg);
	System.out.print("%");
	System.out.println("");

	System.out.println("The median in the class is: " + grades[7]);

	}

	// Method bubbleSort
	public void bubbleSort() {
	// Initializing and declaring variables
	double fNow;
	int count;
	int numbers;

	// Starting loop to sort
	for (count = 0; count < grades.length - 1; count++) {
		// Starting loop inside of sort loop for total list
		for (numbers = 0; numbers < grades.length - 1 - count; numbers++)

		// Declaring how list is formatted
		if (grades[numbers] > grades[numbers + 1]) {
			// Declaring new order of list
			fNow = grades[numbers];
			grades[numbers] = grades[numbers + 1];
			grades[numbers + 1] = fNow;
		}
	}
	}

	public STUDENT_GRADES(int n) {
	// int totStudents = 15;

	grades = new double[n];

	for (int i = 0; i < grades.length; i++) {
		grades[i] = random.nextDouble() * 100;
	}
	}

	public String toString() {
	String output = "";
	for (int i = 0; i < grades.length; i++) {
		output += "Student number " + (i + 1) + "'s grade is: " + grades[i]
			+ "\n";

	}
	return output;
	}

	public double average() {
	// write the code to calculate the average here
	}

	public double highestGrade() {
	// code to find the highest value
	}
}



Thank you very much, you have helped me soooooooooo much!!!! I really appreciate it!!!!!!!! :^: :D
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: I need to create more methods

Posted 22 September 2009 - 05:15 PM

no problem, glad that i could help :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1