• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with simple Java program?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic