• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with simple Java program?  RSS feed

 
Abra Cooper
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my first Java assignment ever (due in a couple hours), I have to create this:

1. Write a java class Student (file: Student.java) that represents a student. The class should have student name, student email as instance variables. It should also contain a no-argument constructor that assigns both name and email with value "UNASSIGNED". It should also implement 5 methods:
-- getEmail: returns the email of the student,
-- getName: returns the name of the student,
-- setEmail: sets the email of the student to given parameter value, and
-- setName: sets the name of the student to given parameter value.
-- toString: print Details of the object created (in this case the name and the email)

2. Write a java class Tester (file: Tester.java). In this class you create/construct a student object. And print the object info by calling toSting() method.

~~~

This is what I've created so far, but I'm getting errors ("Non-static method cannot be referenced from static content") and I don't really know what to do next. Any help would be greatly appreciated!

public class Student
Java.png
[Thumbnail for Java.png]
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome on the Ranch!

On lines 38 and 39 you are calling non-static methods getName() and getEmail() from within a static method main().
This is not allowed.

You need to create an instance first and then call those methods on the instance. Like:
Also, as you wrote the toString() method you may want to test it:
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you writing UNASSIGNED when you have input of name/email in the constructor?
Please always tell us what the question is about in its subject line. Use the subject to attract attention.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, a no-argument constructor is one that has, well, no arguments/parameters. How many arguments have you declared on line 5?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You may want to go with constructors similar way as this:

2. In case name is: 'Abra Kadabra' and email 'abra@kadabra.ac.uk', what the output would be after the toString() method gets invoked?

3. Try not to put space character before opening brace if it is a method or constructor.
better it would be as:

Put space characters after the 'if', 'else if', 'while', 'for'.

4. Your main method supposed to be in other class, named - Tester, rather than in the Student.

[addition] I'm not sure if you need to create constructor with arguments. It seems instructions don't ask it. Probably keep only no-argument constructor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!