问题
I am in a programming class that has provided me with a project but I have no idea where to start and was hoping someone could push me in the right direction. I am only posting part of the project so that someone can show me a bit of the code to get an idea of how its done as I have taken a programming class before but I am out of practice.
Create an application called Registrar that has the following classes:
A Student class that minimally stores the following data fields for a student:
- Name
- Student id number
- Number of credits
The following methods should also be provided:
- A constructor that initializes the name and id fields
- A method that returns the student name field
- Methods to set and retrieve the total number of credits
I have removed most of the question as I am not trying to get the full answer but to just get this little sample to try to get going on the rest of the project. I am also having trouble with the 2nd part as to how I can create names and ID's on a second program and retrieve them into the first program with the classes.
回答1:
Here is a bit of an translation for what you need to do, the words in bold are keywords that, when googled, will most likely return information relevant to what you are doing.
A Student class that minimally stores the following data fields for a student:
This basically means to create a class which has the following properties:
• Name • Student id number • Number of credits
Think hard about what types of data those would be? What type do you need to create to store somebody's name? Or their Id? Remember, these are all properties
A constructor that initializes the name and id fields
Google constructor and learn all about how they work, pay special attention when a learning source discusses how to initialize properties inside of the constructor.
A method that returns the student name field
Research about methods and how you can create one to return your property Student Name. Learn how you will actually use this method.
Methods to set and retrieve the total number of credits
Research Getters and Setters and understand how they interact with a classes properties
Best of luck buddy, google is your best friend/lover in programming..
回答2:
public class Student {
private String name;
private String id;
private int numOfCredits;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getNumOfCredits() {
return numOfCredits;
}
public void setNumOfCredits(int numOfCredits) {
this.numOfCredits = numOfCredits;
}
}
来源:https://stackoverflow.com/questions/25924690/how-to-create-a-java-class-with-data-fields