Java Help Please!!!!

Ruffian998

Member
Feb 9, 2005
25
0
0
I have to make a program that uses two files. The first program will include this:

name
GPA
setName(String sName)
getName()
setGPA(float gpa)
getGPA()

the second program will:

declare a new studnet with the name Robert
Print the Student Name
Change students name to Frank
Print the student name
Ask the user to input the gpa
Print the student gpa.


I'm having a tough time with the first program, once I get to the setName(String sName) I have no clue what to do, as I have yet to learn about this.

This is what I have so far:

class Student
{
String name;
float gpa;

//Question!!! No so I can use the studnet name in the next program do I say the following? This is what I understand from setName(String sName).

public Student
{
name = Robert;
}



--------


Also, if that is correct, do I do the same for setGPA? What do I do for getGPA? Thanks to anyone who helps, I have exhausted all my other resources, I have no one else to turn to for help.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Instead of doing:

public Student
{
name = Robert;

----

Should I :

String setName(String Robert);

---

Only thing is, when I do this, I get a missing method body, or declare abstract..
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
That's cause you don't have a method body...
A typical setter looks like this:

public void setName(String name) {
this.name = name;
}

Who's teaching you the syntax?
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Ok I think i might have answered my own question for half of the first part, here is what I have so far:

class Student
{
String name;
float gpa;

public void setName (String name)
{
sname = "Robert";
}
public String getName ()
{
return sname;
}

String sname = "Robert";
}


-----

So far, everything has compiled correctly.....
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
get() and set() methods are generally a JavaBean concept for properties. Normally you have a private data member for a class, and expose it for reading and/or writing by creating public methods.

For instance, I can have a class called Rectangle with two private data members: width and height.

public class Rectangle{
...private int width;
...private int height;
}

I can expose these data members by adding get() and set() functions:

public class Rectangle{
...private int width;
...private int height;

...public int getWidth(){
......return width;
...}

...public void setWidth(int iWidth){
......width = iWidth;
......return;
...}

...public int getHeight(){
......return height;
...}

...public void setHeight(int iHeight){
......height = iHeight;
......return;
...}
}

Once I have that, I can call these methods from other classes to get and set the properties of my Rectangle class.

oRect.setWidth(100);
oRect.getWidth();

Hope this helps.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
Ok I think i might have answered my own question for half of the first part, here is what I have so far:

class Student
{
String name;
float gpa;

public void setName (String name)
{
sname = "Robert";
}
public String getName ()
{
return sname;
}

String sname = "Robert";
}


-----

So far, everything has compiled correctly.....

Don't set the name to "Robert" within your Student class. Make Student generically accept any value for the name property, and set the name to Robert in your second program once you create the Student object.

Get rid of the sname variable entirely and have getName() and setName() manipulate your name property.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
MrChad are you talking about something like this?

class Student
{
String name;
float gpa;

public void setName ()
{
}
public String getName ()
{
return name;
}


}
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You put return statements at the end of void methods? I don't think I've ever seen that before :p
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: kamper
You put return statements at the end of void methods? I don't think I've ever seen that before :p

It's optional. :p
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
MrChad are you talking about something like this?

class Student
{
String name;
float gpa;

public void setName ()
{
}
public String getName ()
{
return name;
}


}

Yes, but setName should accept a string argument and use that to update the value of name.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
so are you saying that it should be:

public void setName (String sName)


???

I'm not quite sure I follow that one.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Look at my example above:

public void setWidth(int iWidth){
...width = iWidth;
}

The iWidth parameter is used to update the value of the width data member.

Follow the same logic for your sName parameter and name data member.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
I think i understand that now, after looking back at that other post.

My question now is, where did you get the following that you posted at the end of that post:

oRect.setWidth(100);
oRect.getWidth();


I'm not seeing where you got oRect at, I can tell where you got setWidth and getWidth, but not the oRect.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
I think i understand that now, after looking back at that other post.

My question now is, where did you get the following that you posted at the end of that post:

oRect.setWidth(100);
oRect.getWidth();


I'm not seeing where you got oRect at, I can tell where you got setWidth and getWidth, but not the oRect.

Ok, for my second class (let's assume this is a console application) I might have:

class MainApp{
...public static void main(String[] args){
......Rectangle oRect = new Rectangle;
......int iWidth;

......oRect.setWidth(100);
......iWidth = oRect.getWidth();
...}
}

I create a new Rectangle object (called oRect) and use the get() and set() methods to manipulate its properties.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
does that go on the first or second program?

Second program (the main application).

Is this your first Java project? Do you understand the basics of object oriented programming? You may want to look at the Java tutorial so you can start to understand the basics.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Yeah this is my first java project. I had trouble using two programs to creat one in Qbasic as well. If its just one program that I'm working on, im usually fine. BTW, thanks for all your help, you have no clue what it means to me. I'm really learning a lot from this too as im going along.

This is what I have:

class StudentMain
{
public static void main(String[] args)
{

Student oStud = new Student;
String sName;

oStud.setName("David");
sName = oStud.getName();
}
}


When I went to compile it, it said that ( or [ is expected at Student oStud = new Student;

Also, will I not need to use these?

import java.io*;

or

throws IOException

 

Ruffian998

Member
Feb 9, 2005
25
0
0
do you have AIM?

edit: if you do and you are willing to help me through that way, just PM me.

Again, thanks....
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Make sure your StudentMain.class and Student.class are in the same directory, otherwise StudentMain won't know how to create a Student object.

You will need import java.io.*; to output to the console (printing student name and gpa) and receive input as well (input the gpa).

EDIT: Also, change your

Student oStudent = new Student;

to

Student oStudent = new Student();

Forgot the parentheses in my earlier example.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Alright, I got all of that to print out thank you.

No back to the first program. When I put in

public void setGPA (float gpa)
{
GPA = gpa;
return;
}

public String getGPA ()
{
return GPA;
}


it says that I have an imcompatible type at the return statment line.

On the second program, when I have the user input the gpa, how will I use it from the first program, I dont really see why it would be needed in the first program at all really.

Shouldnt it be something like this:

float a;

BufferedReader inData = new BufferedReader (new InputStreamReader (System.in));

System.out.print("Enter gpa: ");
String temp = inData.readLine ();
a = Float.parseFloat(temp);

if (a > 0 & a <4)
System.out.println("The Student's GPA is " + a);

else
System.out.println("The Student's GPA is 3.0");
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
Alright, I got all of that to print out thank you.

No back to the first program. When I put in

public void setGPA (float gpa)
{
GPA = gpa;
return;
}

public String getGPA ()
{
return GPA;
}


it says that I have an imcompatible type at the return statment line.

Your return type should be float not String.

Originally posted by: Ruffian998
On the second program, when I have the user input the gpa, how will I use it from the first program, I dont really see why it would be needed in the first program at all really.

Shouldnt it be something like this:

float a;

BufferedReader inData = new BufferedReader (new InputStreamReader (System.in));

System.out.print("Enter gpa: ");
String temp = inData.readLine ();
a = Float.parseFloat(temp);

if (a > 0 & a <4)
System.out.println("The Student's GPA is " + a);

else
System.out.println("The Student's GPA is 3.0");

Your logic is sound, except I think it's implied that you should update the student GPA using the setGPA() method once you receive the user's input.

Make sure you handle the NumberFormatException that Float.parseFloat() can throw.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
yeah it is implied that it should update the setGPA once I recieve the users input.

What is that NumberFormatException i have never heard of that one.

ok, this is what I have now and I get a cannot resolve sysmbol variable gpa on the lines that are bold:


float a;

System.out.println("Enter gpa: ");
String temp = inData.readLine ();
a = Float.parseFloat(temp);

oStudent.setGPA(a);
gpa = oStudent.getGPA();


if (a > 0 & a <4)
System.out.println("The Student's GPA is " + gpa);

else
System.out.println("The Student's GPA is 3.0");



Am I doing that right though? Oh, also the first program compiles correctly, and where I had the BufferedReader in Data....I just moved it up to the top of the program. Before I added the

oStudent.setGPA(a);
gpa = oStudent.getGPA();

Everything worked like a charm. I could even enter a gpa and it would spit it out at me.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
You need to declare gpa.

float a, gpa;

System.out.println("Enter gpa: ");
String temp = inData.readLine ();
a = Float.parseFloat(temp);

oStudent.setGPA(a);
gpa = oStudent.getGPA();


if (a > 0 & a <4)
System.out.println("The Student's GPA is " + gpa);

else
System.out.println("The Student's GPA is 3.0");


You'll want to wrap your user input in a try / catch block to handle bad input:

String temp = inData.readLine();
try{
...a = Float.parseFloat(temp);
}
catch (NumberFormatException e){
...// user entered a non-floating point value
...System.out.println("You entered a non-floating point value!");
...return;
}

You can more fancy (e.g. keep repeating the prompt until the user enters a valid value), or you can just exit the program.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Thank you so much MrChad, I couldnt have done it without you.

BTW, thanks for that link, this weekend when I have a lot of free time, I'm gonna try and read up on that site a lot.

Again thanks a lot...
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Ruffian998
Thank you so much MrChad, I couldnt have done it without you.

BTW, thanks for that link, this weekend when I have a lot of free time, I'm gonna try and read up on that site a lot.

Again thanks a lot...

:thumbsup:

No problem, glad I could help. :)