|
 |
|
 |
09-05-2004, 10:00 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
How to do this question?Need help~
a)Create a class Student which contains the following
Student id
Student name
Center code
Payment
This class will be used to support single link list implementation.Also include the constructor, accesor, and mutator methods for the Student class.
b)
public class StudentList
{
private Student startingnode;
}
You are required to write methods to manipulate the information found in the link list defined above.
Constructor
Add method
Printing method which will display minimum 3 type of student details
Searching method which serach base on student id
SumPayment method which show the total payment made by all the students
Other method
c ) Write a program that demonstrates that you are able to activate all the methods writen above.
Student System
Add new Student movie
Search StudentMovie
Student Report
Total Payment
Others method
Exit
|
|
|
09-06-2004, 04:09 AM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I can't do it in Java. But if you observe my C++ code then you're on your way. I think you'll understand the gist of my code.
Code in link below:
http://codenewbie.com/forum/t2092.html
__________________
|
|
|
09-06-2004, 04:30 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
I never learn c/c++ before~I'can't understand it~Pls help~
|
|
|
09-06-2004, 05:06 AM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Ok, let's take this a step at a time. Give me a specfic question and I'll try to help you. Generally, people on these types of boards won't do homework assignments, so ask specific questions as opposed to "How do I do this problem?"
|
|
|
09-06-2004, 06:21 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
Quote:
Originally posted by Belisarius
Ok, let's take this a step at a time. Give me a specfic question and I'll try to help you. Generally, people on these types of boards won't do homework assignments, so ask specific questions as opposed to "How do I do this problem?"
|
ok~I try to do at section (a)correct me if wrong~(so do u know how to support single lick list,accesor and mutator methods for this class?)
Code:
import java.io.*;
import java.util.*;
public class Student{
private String studentId;
private String studentName;
private int centerCode;
private float payment;
private Student next=null;
public Student(String studentId,String studentName,int centerCode,float payment){
this.studentId=studentId;
this.studentName=studentName;
this.centerCode=centerCode;
this.payment=payment;
next=null;
}
public void setId(String newId){
studentId=newId;
}
public void setName(String newName){
studentName=newName;
}
public void setCenter(int newCenter){
centerCode=newCenter;
}
public void setPayment(float newPayment){
Payment=newPayment;
}
public void setNext(Student nextNode){
next=nextNode;
}
public Student getNext(){
return next;
}
public String getId(){
return studentId;
}
public String getName(){
return studentName;
}
public int getCenter(){
return centerCode;
}
public float getPayment(){
return Payment;
}
}
public class StudentList{
private Student startingnode;
public studentList(){
startingnode=null;
}
public void add(Student newNode){
Student curr;
Student prev;
curr=startingnode;
prev=null;
while(curr!=null){
prev=curr;
curr=curr.getNext();
}
prev.setNext(newNode);
newNode.setNext(curr);
}
}
Last edited by Sunshine; 09-11-2004 at 05:25 AM.
|
|
|
09-06-2004, 07:04 PM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
First off, some syntax errors. This won't compile.
Java is case-sensetive. All instances of "string" should be "String". In Java, all class names start with a capital letter.
Now, your constructor is wrong too.
Code:
public Student(studentId,studentName,centerCode,payment){
Java needs to know what type these variables are. For instance, "studentId" needs to be prefaced with a "String" so that Java knows that "studentId" is a String.
Now, what do you know about Linked Lists, Accessors and Mutators? All are very basic concepts that most students learn in their first year of a CS program (which is where I assume you're at).
|
|
|
09-06-2004, 11:04 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
Quote:
Originally posted by Belisarius
First off, some syntax errors. This won't compile.
Java is case-sensetive. All instances of "string" should be "String". In Java, all class names start with a capital letter.
Now, your constructor is wrong too.
Code:
public Student(studentId,studentName,centerCode,payment){
Java needs to know what type these variables are. For instance, "studentId" needs to be prefaced with a "String" so that Java knows that "studentId" is a String.
Now, what do you know about Linked Lists, Accessors and Mutators? All are very basic concepts that most students learn in their first year of a CS program (which is where I assume you're at).
|
ok..I update the code agian~pls check for me.
|
|
|
09-07-2004, 04:02 AM
|
#8 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Ok, you cleared up most of the syntax errors, but you overdid the capitalization. In Java, objects start with a capital letter, but not primitives, such as int or double. You need to lowercase all the primitive types. Hence, "Int i" would become "int i". And it looks like you got all the accessors and mutators right.
|
|
|
09-07-2004, 10:31 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
Quote:
Originally posted by Belisarius
Ok, you cleared up most of the syntax errors, but you overdid the capitalization. In Java, objects start with a capital letter, but not primitives, such as int or double. You need to lowercase all the primitive types. Hence, "Int i" would become "int i". And it looks like you got all the accessors and mutators right.
|
I have update the code agian,and the link list.Pls check for me,and I dun know what to do next step~
|
|
|
09-08-2004, 04:04 AM
|
#10 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
You've actually come along quite nicely. Now, tell me what you know about Linked Lists.
|
|
|
09-08-2004, 07:17 PM
|
#11 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
Linked lists..a node that contains data and link to next item.I just know the easy definition.
|
|
|
09-09-2004, 04:44 AM
|
#12 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Yep. You can make things a bit easier on people by making some convience additions to it, such as a constant reference to the head of a list (in fact, a Linked List is usually worthless unless you do this). You also need to add the ability to add and remove items. And you need some ability to iterate throughout the list.
So, you need to keep track of the head of the list, the current position of someone iterating thoughout the list (hint: don't use a number), the ability to remove the current node without breaking the list, and the ability to add a node (something to think about: would you want to add a node before or after the current node?).
|
|
|
09-10-2004, 07:32 PM
|
#13 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
I have update the code,pls check for me.Hmm.. linked list is not easy as the definition that I know.
|
|
|
09-10-2004, 07:54 PM
|
#14 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Don't declare the class within the class, you can do it but it's not something I'd recommend doing because it gets too confusing. I'd put it in another file, or at least a seperate class
Code:
public class Foo {
.
.
.
}
class Bar{
.
.
.
}
Now, in your add method (if you could clean it up a bit, it's hard to follow without the indentation), you have
Code:
public void add(Student newNode){
...
Student newNode=new Student();
...
}
This will throw a compile error as you have just declared the variable newNode twice in the same scope.
Finally, you have this bit of code:
Code:
prev=null;
.
.
.
prev.setNext(newNode);
Any guesses as to what will go wrong here? You'll get a NullPointerException (wish I had the spoiler tags from SomethingAwful right now). You can't call a method on a null object.
You have some conceptual problems as well, but we'll get the basic errors out of the way first.
|
|
|
09-11-2004, 05:35 AM
|
#15 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 9
|
Ok...I have update the code,but I dun understand the last error.I have include the pre=curr; (Hope u can correct me agian)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:12 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|