Need help with school project.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1425 Views - Last Post: 13 February 2014 - 11:40 AM Rate Topic: -----

#1 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Need help with school project.

Posted 13 February 2014 - 09:06 AM

Im not asking for anyone to do this for me. I just need some help on how to get this moving.

For the project I need to have two classes (student and Grades). Im working on the student class and it isnt running any of the code that I have written. I feel like i am missing something really dumb. I am requesting the first and last name of the student and their grades. The program should then print the name and the grades with the overall grade. yet its not printing anything and it isnt even requesting user input.

Please let me know what I am doing wrong. any help would be greatly appreciated.

import java.util.Scanner;

/** this class sets the information for the student
 * 
 * @author 
 *
 */
public class Student 
{
	
	//Holds the name of the student
		private String name;
		//holds the grades
		private int examGrade;
		private int programGrade;
		
		public	static void main(String[] args)
		{
			Student name = new Student();
			
			
		}

		/**sets all attributes of the student
		 *
		 */
		public void setup()
		{

		}

		/**sets the name of the student
		 * 
		 * @param someName
		 */
		private void setName(String someName)
		{
			//initializes user input
			Scanner keyboard = new Scanner(System.in);

			//requests and sets last name
			System.out.println("Please enter students last name:");
			String lastName = keyboard.toString();

			//requests and sets students first name
			System.out.println("Please enter students first name:");
			String firstName = keyboard.toString();

			//sets someName in first and last order
			someName = firstName + " " + lastName;

			//sets someName to name
			name = someName;
		}

		/**sets the grades for the student
		 * 
		 */
		private void setGrades()
		{
			Scanner keyboard = new Scanner(System.in);

			//requests and sets grades
			System.out.println("Please enter " + "'s program and exam grades:");

			programGrade = keyboard.nextInt();
			examGrade = keyboard.nextInt();

		}

		/**displays the complete information on the student
		 * 
		 */
		public void display()
		{
			System.out.println(name + " has grades of " + examGrade + ", " + programGrade + ".");
		}

		/**returns the overall grade of the student
		 * 
		 * @return
		 */
		public double overallGrade()
		{
			final double PROGRAM_WEIGHT = 0.40;
			final double EXAM_WEIGHT = 1 - PROGRAM_WEIGHT;

			double theOverallGrade;

			theOverallGrade = programGrade * PROGRAM_WEIGHT + examGrade * EXAM_WEIGHT;

			return theOverallGrade;
		}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with school project.

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Need help with school project.

Posted 13 February 2014 - 09:09 AM

All your main method does is to create a new Student - it does nothing else.
Was This Post Helpful? 0
  • +
  • -

#3 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 09:11 AM

so just make it say new Student?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Need help with school project.

Posted 13 February 2014 - 09:15 AM

Quote

and it isnt running any of the code that I have written. I feel like i am missing something really dumb. I am requesting the first and last name of the student and their grades. The program should then print the name and the grades with the overall grade. yet its not printing anything and it isnt even requesting user input.


None of that is happening because, in your main, you are only creating the student object and that's it.


17	        public  static void main(String[] args)
18	        {
19	            Student name = new Student();
20	             
21	             
22	        }


This should be outside of your student class.. it's the main-thang of your app.. It's the starting line.. where the app goes to first when you run it.
Was This Post Helpful? 0
  • +
  • -

#5 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 09:18 AM

Forgive me if im misunderstanding but when i move my main method outside of my class it errors out
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Need help with school project.

Posted 13 February 2014 - 09:20 AM

Typically it helps to tell folks what errors you are getting.
Was This Post Helpful? 0
  • +
  • -

#7 Golossos   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 118
  • Joined: 29-June 13

Re: Need help with school project.

Posted 13 February 2014 - 09:22 AM

Your Student class needs a constructor, which will initialize the fields.

Example:

public Student(String name, int examGrade, int programGrade)
{
    this.name = name;
    this.examGrade = examGrade;
    this.programGrade = programGrade;
}



The "this" keyword is used as a placeholder to refer to the instance of the object. The constructor is called every time you create an instance of a class (like new Student()).

Of course, this constructor "method" will be outside your main method, like any other method.

This post has been edited by Golossos: 13 February 2014 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#8 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 09:23 AM

With the way my code is written i am not getting any errors. it is just not prompting the user at all. This is my first program written from scratch and I am generally confused on why its not working. I wish it was erroring in a way that i could see because then i could troubleshoot it but its not.
Was This Post Helpful? 0
  • +
  • -

#9 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 09:30 AM

so i have this now and it is telling me to make the grades static.
public	static void main(String[] args)
	{
		Student name = new Student(name, examGrade, programGrade);
	}
	public Student(String name, int examGrade, int programGrade)
	{
		this.name = name;
		this.examGrade = examGrade;
		this.programGrade = programGrade;
	}



Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Need help with school project.

Posted 13 February 2014 - 09:33 AM

Again.. your main should not.. and I repeat *NOT* be in with your student class's functions.
Was This Post Helpful? 0
  • +
  • -

#11 Golossos   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 118
  • Joined: 29-June 13

Re: Need help with school project.

Posted 13 February 2014 - 09:38 AM

In object-oriented, you want your object class set method to be in its own class, and the test (client) class simply handling the class. For example, you can have:

class Student()
{

}

public void setName(String newName)
{
    name = newName;
}



And other get/set methods will be inside the Student class. Then, you would have a test class (or client class), that will interact with the Student class, as well as create an instance of a Student. This class will have the Scanner object and handle the input. The input will then get passed to the constructor to the Student instance, like so:

class StudentApp
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        String name = keyboard.next();

        Student s = new Student(s, grade, grade);
    }
}


This post has been edited by Golossos: 13 February 2014 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#12 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 09:38 AM

I appreciate that your trying to help but i am failing to understand. are you telling me to put it above public class Student?
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Need help with school project.

Posted 13 February 2014 - 09:51 AM

Hell - put them in two separate files. One call one main and put your main in there.. and the second file 'student' and put your student class there.
Was This Post Helpful? 0
  • +
  • -

#14 drunken   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 14

Re: Need help with school project.

Posted 13 February 2014 - 10:08 AM

so I have this now and it is prompting and receiving user input. but it is not printing back. The methods that I have declared are required for this project so I will need to find a way to work them in. What is supposed to be happening is that all the things related to setting the name go in the setName method and all the hings to do with grades go in the getGrades method. btw im sorry if im being a pain i really appreciate the help


import java.util.Scanner;

/** this class sets the information for the student
 * 
 * @author
 *
 */
public class Student 
{
	//Holds the name of the student
	private String name;
	//holds the grades
	private int examGrade;
	private int programGrade;

	public	static void main(String[] args)
	{
				//initializes user input
				Scanner keyboard = new Scanner(System.in);

				//requests and sets last name
				System.out.println("Please enter students last name:");
				String lastName = keyboard.next();

				//requests and sets students first name
				System.out.println("Please enter students first name:");
				String firstName = keyboard.next();

				//sets someName in first and last order
				String someName = firstName + " " + lastName;
		
				//requests and sets grades
				System.out.println("Please enter the grades of " + someName + ":");

				int programGrade = keyboard.nextInt();
				int examGrade = keyboard.nextInt();
				
				Student newStudent = new Student(someName, examGrade, programGrade);
	}
	public Student(String name, int examGrade, int programGrade)
	{
		this.name = name;
		this.examGrade = examGrade;
		this.programGrade = programGrade;
	}


	/**sets all attributes of the student
	 *
	 */
	public void setup()
	{

	}

	/**sets the name of the student
	 * 
	 * @param someName
	 */
	private void setName(String someName)
	{
		//sets someName to name
		name = someName;
	}

	/**sets the grades for the student
	 * 
	 */
	private void setGrades()
	{
		
	}

	/**displays the complete information on the student
	 * 
	 */
	public void display()
	{
		System.out.println(name + " has grades of " + examGrade + ", " + programGrade + ".");
	}

	/**returns the overall grade of the student
	 * 
	 * @return
	 */
	public double overallGrade()
	{
		final double PROGRAM_WEIGHT = 0.40;
		final double EXAM_WEIGHT = 1 - PROGRAM_WEIGHT;

		double theOverallGrade;

		theOverallGrade = programGrade * PROGRAM_WEIGHT + examGrade * EXAM_WEIGHT;

		return theOverallGrade;
	}

}


Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Need help with school project.

Posted 13 February 2014 - 11:04 AM

Okay.. so you are just avoiding the commentary about *NOT* putting your main inside your class you are trying to create. Fine.. grand.. but it's wrong.

Here's how the setup should go.

I have a file call 'foo.java', and it looks like this:
public class foo
{

	private int _bar;// private variable.
	
	public foo()
	{
	// constructor.
		_bar = 0;
	}
	
	public foo(int input)
	{
	// constructor with input
		_bar = input;
	}

	// function to interact with private variable.
	public int Add(int input)
	{
		return _bar + input;
	}
}



See - it's all there, and importantly there is no 'main' in it.


I have a second file called 'main.java'. Here's where the program starts and runs. *NOT* in the 'foo' class, but here.

 
public class main
{
	public static void main(String[] args)
	{
	 System.out.println("Hello World!");// to verify it is running
		foo derp = new foo(2); // create foo object
		System.out.println(derp.Add(5));// call a function inside of it.
		
	}

}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2