Syntax error with variable - java

I am having errors below and I am confused why this is a Lab to make a storage spot for a students information.
import java.util.Arrays;
I have a problem with the square brackets below
public class Student {
private String studentID;
private String studentName;
private String studentMajor;
private double studentGPA;
private String studentGrad;
private long[] ;
And a problem with Student() below
public Student(){
studentID = "";
studentName = "";
studentMajor = "";
studentGPA = 0;
studentGrad = "";
}
public Student(String stuID, String stuName, String stuMajor, double stuGPA, String stuGrad) {
studentID = stuID;
studentName = stuName;
studentMajor = stuMajor;
studentGPA = stuGPA;
studentGrad = stuGrad;
}
public void setID(String ID) {
studentID = ID;
}
public void setName(String name) {
studentName = name;
}
public void setMajor(String major) {
studentMajor = major;
}
public void setGPA(double GPA) {
studentGPA = GPA;
}
public void setGrad(String Grad) {
studentGrad = Grad;
}
public String getID() {
return studentID;
}
public String getName() {
return studentName;
}
public String getMajor() {
return studentMajor;
}
public double getGPA() {
return studentGPA;
}
public String getGrad() {
return studentGrad;
}
public void printData() {
System.out.println("Student ID: "+studentID);
System.out.println("Student Name: " +studentName );
System.out.println("Student Major: "+studentMajor);
System.out.println("Student GPA: "+ studentGPA);
System.out.println("Student Year of Graduation: " +studentGrad);
}
}

private long[]; is incorrect syntax.
To complete the variable you must insert an identifier (the variable name).
In most IDE's (i.e. Eclipse and Netbeans), this incomplete variable will cause the next line to have issues. This is why public Student(){ (despite being completely valid code) is giving you an error. If you temporarily delete the line private long[]; you should see this error go away.
From the code you supplied, it looks like private long[]; can be deleted anyway as an array isn't being used.

Related

How do I assign object from one class to another class and how do I calculate double and int inside an ArrayList?

I have an assignment but i have problem trying to do some part of it. Appreciate if anyone can give me a hand.
Assignment brief: https://pastebin.com/3PiqvfTE
Main Method: https://pastebin.com/J2kFzB3B
import java.util.ArrayList;
import java.util.Scanner;
public class mainMethod {
public static void main(String[] args) {
//scanner
Scanner scanner = new Scanner (System.in);
//subject object
Subject subject = new Subject (0,null,0);
System.out.println("How many subject do you want to enter: ");
int subjectNumber = scanner.nextInt();
for (int j = 0; j < subjectNumber; j++) {
//subject ID
System.out.println("Please enter the subject ID: ");
int subID = scanner.nextInt();
//subject Name
System.out.println("Please enter subject name: ");
String subName = scanner.next();
//subject fee
System.out.println("Please enter subject fee: ");
double subFee = scanner.nextDouble();
//add subject
subject.addSubject(subID, subName, subFee);
}
//display subject
System.out.println(subject.getSubject());
/*
//loop for part time teacher
System.out.println("Please enter how many part time teacher do you have: ");
int PTcounter = scanner.nextInt();
for (int i = 0; i < PTcounter; i++) {
*/
Teacher teach = new Teacher (0,null,null);
//teacher employee ID
System.out.println ("Please enter the teacher employee ID: ");
int tid = scanner.nextInt();
//teacher name
System.out.println("Please enter the teacher name: ");
String tname = scanner.next();
//teacher gender
System.out.println("Please enter the teacher gender: ");
String tgender = scanner.next();
//add teacher details
teach.addTeacher(tid, tname, tgender);
//display teacher details
System.out.println(teach.getTeacher());
//call address class
Address address = new Address (0,null,null,0);
//address house number
System.out.println("Please enter house number: ");
int addyNum = scanner.nextInt();
//address street name
System.out.println("Please enter street name: ");
String StreetName = scanner.next();
//address city
System.out.println("Please enter city: ");
String City = scanner.next();
//address post code
System.out.println("Please enter postcode: ");
int Postcode = scanner.nextInt();
//add address
address.addAddress(addyNum, StreetName, City, Postcode);
//display address
System.out.println(address.getAddress());
//call Part Time Salary class
PartTimeSalary ptSalary = new PartTimeSalary(0,0);
//max hours
System.out.println("Please enter maximum work hours: ");
int maxHours = scanner.nextInt();
//hourly rate
System.out.println("Please enter hourly rate: ");
double hourlyRate = scanner.nextDouble();
ptSalary.addPTSalary(maxHours, hourlyRate);
System.out.println(ptSalary.getHourlyRate());
//System.out.printf("Teacher details is %s, Subject details is %s, Address is %s", teach.toString(),subject.toString(),address.toString());
}
}
1st problem. I have a subject class and a teacher class. Teacher will be able to teach maximum of 4 subject. I have prompt user to enter subject details before entering teacher details, so when user enter teacher details they will be able to assign subject to teacher just by using the subjectID. I have problem implementing this. My subject is already store in an ArrayList but how to I connect this with teacher. And in the end the program will display what subject each teacher teaches.
Subject Class: https://pastebin.com/iBYFqYDN
import java.util.ArrayList;
import java.util.Arrays;
public class Subject {
private int subjectID;
private String subjectName;
private double fee;
private ArrayList <Subject> subjectList = new ArrayList <Subject>();
public Subject(int subjectID, String subjectName, double fee) {
this.subjectID = subjectID;
this.subjectName = subjectName;
this.fee = fee;
}
public int getSubjectID () {
return subjectID;
}
public void setSubjectID (int subjectID) {
this.subjectID = subjectID;
}
public String getSubjectName () {
return subjectName;
}
public void setSubjectName (String subjectName) {
this.subjectName = subjectName;
}
public double getFee () {
return fee;
}
public void setFee (double fee) {
this.fee = fee;
}
#Override
public String toString() {
return "[subjectID=" + subjectID + ", subjectName=" + subjectName + ", fee=" + fee + "]";
}
public void addSubject (int subjectID, String subjectName, double fee) {
subjectList.add(new Subject(subjectID, subjectName, fee) );
}
public String getSubject () {
return Arrays.toString(subjectList.toArray());
}
}
Teacher class: https://pastebin.com/Np7xUry2
import java.util.ArrayList;
import java.util.Arrays;
public class Teacher {
private int employeeID;
private String name;
private String gender;
private ArrayList <Teacher> teacherDetailsList;
public Teacher (int employeeID, String name, String gender) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.teacherDetailsList = new ArrayList <Teacher>();
}
public int getEmployeeID () {
return employeeID;
}
public void setEmployeeID (int employeeID) {
this.employeeID = employeeID;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getGender () {
return gender;
}
public void setGender (String gender) {
this.gender = gender;
}
#Override
public String toString() {
return "[employeeID=" + employeeID + ", name=" + name + ", gender=" + gender + "]";
}
public void addTeacher (int employeeID, String name, String gender) {
teacherDetailsList.add(new Teacher (employeeID,name,gender));
}
public String getTeacher () {
return Arrays.toString(teacherDetailsList.toArray());
}
}
2nd problem. Teacher will either be part time or full time teacher. Part time teacher will have a maximum work hour they can work and an hourly rate they will be paid for, so the final salary of part time teacher will be "maximum hours" multiply by "hourly rate". I have store "hourly rate" and "maximum work hours" in an ArrayList but how do I call them to make the multiplication then displaying at the end.
Part time salary class: https://pastebin.com/iGKpu87Y
import java.util.ArrayList;
public class PartTimeSalary {
private int maxHour;
private double hourlyRate;
private double hourlySalary;
private ArrayList <PartTimeSalary> PTSalary = new ArrayList <PartTimeSalary>();
private ArrayList <Double> finalHourlySalary = new ArrayList <Double>();
public PartTimeSalary (int maxHour, double hourlyRate) {
this.maxHour = maxHour;
this.hourlyRate = hourlyRate;
}
public int getMaxHours () {
return maxHour;
}
public void setMaxHour (int maxHour) {
this.maxHour = maxHour;
}
public double getHourlyRate () {
return hourlyRate;
}
public void setHourlyRate (double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getHourlySalary() {
hourlySalary = hourlyRate*maxHour;
return hourlySalary;
}
public void addPTSalary (int maxHour, double hourlyRate) {
PTSalary.add(new PartTimeSalary(maxHour,hourlyRate));
}
public void FinalHourlySalary (double hourlySalary) {
hourlySalary = hourlyRate * maxHour;
finalHourlySalary.add(hourlySalary);
}
public ArrayList<Double> getFinalSalary () {
return (new ArrayList <Double>());
}
}
3rd question. I have an address class which is suppose to be part of the Teacher class. I can't seem to connect the address class with teacher class.
Address class: https://pastebin.com/s2HN5p80
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Address {
private int houseNum;
private String streetName;
private String city;
private int postcode;
private List <Address> address = new ArrayList <Address>();
public Address (int houseNum, String streetName, String city, int postcode) {
this.houseNum = houseNum;
this.streetName = streetName;
this.city = city;
this.postcode = postcode;
}
public int getHouseNum() {
return houseNum;
}
public void setHouseNum (int houseNum) {
this.houseNum = houseNum;
}
public String getStreetName() {
return streetName;
}
public void setStreetName (String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity (String city) {
this.city = city;
}
public int getPostcode() {
return postcode;
}
public void setPostcode (int postcode) {
this.postcode = postcode;
}
public void addAddress (int houseNum, String streetName, String city, int postcode) {
address.add(new Address (houseNum,streetName,city,postcode));
}
#Override
public String toString() {
return "[houseNum=" + houseNum + ", streetName=" + streetName + ", city=" + city + ", postcode="
+ postcode + "]";
}
public String getAddress () {
return Arrays.toString(address.toArray());
}
}
Thank you 😃😊
Question 1)
Put the "teacherDetailsList" and "subjectList" ArrayLists in the main method, not the consructors. Add the teachers and subjects in main methods, not constructors.
Add a new ArrayList called "mySubjects" as a field for the Teacher Class
Add the following 2 method to the Teacher class:
public Teacher (int employeeID, String name, String gender, ArrayList<Subject> s) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.mySubjects=s;
}
public Teacher (int employeeID, String name, String gender, Subject s) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.mySubjects.add(s);
}
public void addSubject(Subject s, boolean b){
if(mySubjects.size()<4)mySubjects.add(s);
else throw OutOfBoundsError;
}
public void removeSubject(Subject s, boolean b){
mySubject.remove(s);
}
public void addSubject(Subject s){
s.changeTeacher(s);
}
public void removeSubject(Subject s){
mySubject.remove(s);
}
Add the following methods & fields to Student class
private Teacher teacher;
public Subject(int subjectID, String subjectName, double fee, Teacher t) {
this.subjectID = subjectID;
this.subjectName = subjectName;
this.fee = fee;
this.setTeacher(t);
}
public void setTeacher(Teacher t){
try{
t.addSubject(this);
teacher=t;
}
catch(OutOfBoundsError e){
System.out.println(t.getName()+" already has a full schedule.");
}
}
public void changeTeacher(Teacher t){
teacher.removeSubject(this);
teacher = t;
t.addSubject(this);
}
Question 2)
make a new double field, constructor parameter (for every constructor in the class), mutator method and accessor mutator called maxHour in the class Subject
add a double field to the teacher class called salary. If the teacher is part-time, set this to 0 in the constructor.
add this method to Teacher class:
public double getSalary(){
if(salary!=0) return salary;
double s=0;
for(Subject i: mySubjects) s+=i.getFee()*i.getMaxHours();
return s;
}
You honestly no longer need the PartTimeSalary class now.
Question 3
make a new Address field, constructor parameter (for every constructor in the class), mutator method and accessor mutator called myAddress in the class Teacher
Don't bother with the ArrayList stuff in your Address Class

Encapsulation (User Input)

//this is my main class
import java.io.*;
import java.util.*;
public class TheInnovator{
private String name;
private String age;
private String designation;
private String course;
private String yrlvl;
public TheInnovator(String name, String age, String designation, String course, String yrlvl){
this.name = name;
this.age = age;
this.designation = designation;
this.course = course;
this.yrlvl = yrlvl;
}
public void setName(String name){
this.name = name;
}
public void setAge(String age){
this.age = age;
}
public void setDesignation (String designation){
this.designation = designation;
}
public void setCourse(String couse){
this.course = course;
}
public void setYrlvl (String yrlvl){
this.yrlvl = yrlvl;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getDesignation(){
return designation;
}
public String getCourse(){
return course;
}
public String getYrlvl(){
return yrlvl;
}
}
// this is my Main Driver
import java.util.*;
public class MainDriver{
public static void main(String args[]){
TheInnovator theinnov = new TheInnovator();
Scanner input = new Scanner(System.in);
theinnov.setName = (input.nextLine());
theinnov.setAge = (input.nextLine());
theinnov.setDesignation = (input.nextLine());
theinnov.setCourse = (input.nextLine());
theinnov.setYrlvl = (input.nextLine());
System.out.println("Name: " + theinnov.getName());
System.out.println("\nAge: " + theinnov.getAge());
System.out.println("\nDesignation: " + theinnov.getDesignation());
System.out.println("\nCourse: " + theinnov.getCourse());
System.out.println("\nYear Level: " + theinnov.getYrlvl());
}
}
So my problem is everytime I run the MainDriver.java, it cannot find my setter variables. what's wrong or what's missing in my code? thank you for a quik response! anyway I'm using notepad++ on this because it's a requirement.
You need to remove the = sign. It makes the compiler think that setName is a field and not a method.
theinnov.setName (input.nextLine());
class Quick {
public static void main(String[]args) {
Don op=new Don();
Scanner sc=new Scanner(System.in);
System.out.println("enter");
op.setName(sc.nextLine());
System.out.println("Name: " + op.getName());
}
}
class Don
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
}
Output: Name: loyal

Null values when printing out arrayList?

Hi I have created a toStringmethod in one of my classes which can be seen below.
Student Class:
package Practical5;
public class Student extends Person {
//instance variables
private static int MAX_MODULES = 6;
private StudentMode modeOfStudy;
private boolean studentLoan;
private int numEnrolledModules;
//constructor
public Student(String name, String dob, Address address, StudentMode modeOfStudy, boolean studentLoan) {
super(name, dob, address);
this.modeOfStudy = modeOfStudy;
this.studentLoan = studentLoan;
this.numEnrolledModules = 0;
}
//accessors & mutators
public StudentMode getMode() {
return modeOfStudy;
}
public boolean isStudentLoan() {
return studentLoan;
}
public int getNumEnrolledModules() {
return numEnrolledModules;
}
public void setMode(StudentMode modeOfStudy) {
this.modeOfStudy = modeOfStudy;
}
public void setStudentLoan(boolean studentLoan) {
this.studentLoan = studentLoan;
}
public void setNumEnrolledModules(int numEnrolledModules) {
this.numEnrolledModules = numEnrolledModules;
}
#Override
public void purchaseParkingPass() {
System.out.println(getName() + " just purchased a parking pass with student discount.");
}
#Override
public void addModule(String moduleCode) {
if (getNumEnrolledModules() < MAX_MODULES) {
System.out.println(getName() + " successfully registered for the module: " + moduleCode);
}
else {
System.out.println("You are unable to register for " + moduleCode + " as the maximum number of permitted module enrolments has been reached.");
}
}
public String toString() {
return "Student [ ID: " + getId() + "; Name: " + getName() +
"; DOB: " + getDob() + "; Study Mode: " + getMode() +
"; Number of Enrolled Modules: " + getNumEnrolledModules();
}
}
Person Class:
package Practical5;
public abstract class Person {
//instance variables
private static int LAST_ID = 1000 + 1;
private int id;
private String name;
private String dob;
private Address address;
//constructor
public Person(String name, String dob, Address address) {
super();
LAST_ID ++;
this.id = LAST_ID;
}
//accessors & mutators
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDob() {
return dob;
}
public Address getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setDob(String dob) {
this.dob = dob;
}
public void setAddress(Address address) {
this.address = address;
}
//methods
public abstract void purchaseParkingPass();
public abstract void addModule(String moduleCode);
}
I then created a tester class and created a new ArrayList and added these elements to it.
I then created a for loop in order to loop through each element and call the toString method to print out the details of each element but it is returning null values.
Tester Class:
package Practical5;
import java.util.ArrayList;
import java.util.Scanner;
public class UIS_Tester {
public static void main(String[] args) {
Student student1 = new Student("James Black", "07/09/1995" , new Address("Wheeler's Road",10,"Belfast", "BT12 5EG", "Co.Antrim"),StudentMode.Fulltime, false);
Student student2 = new Student("Adam Smith", "12/11/1979" , new Address("Ivy Hill",67,"Belfast", "BT17 7BN", "Co.Antrim"),StudentMode.Parttime, true);
ArrayList<Person> uniPeople = new ArrayList<Person>();
uniPeople.add(student1);
uniPeople.add(student2);
printMenu(uniPeople);
}
public static void printAllDetails(ArrayList<Person> uniPeople) {
for (int i = 0; i < uniPeople.size(); i++) {
System.out.println(uniPeople.get(i).toString());
}
}
}
Output:
Student [ ID: 1002; Name: null; DOB: null; Study Mode: Fulltime; Number of Enrolled Modules: 0
Student [ ID: 1003; Name: null; DOB: null; Study Mode: Parttime; Number of Enrolled Modules: 0
Can anyone help me with this problem? Thanks
public Person(String name, String dob, Address address) {
super();
LAST_ID ++;
this.id = LAST_ID;
}
The constructor completely ignores its three arguments. It doesn't assign them to the corresponding fields, so these fields keep their default value: null.
You have to store the name value in the constructor. Your version did not use the name value.
public Person(String name, String dob, Address address) {
super();
this.name = name; // <== important line
this.dob = dob; // <== important line
this.address = address; // <== important line
LAST_ID ++;
this.id = LAST_ID;
}
Look at the constructor in person and in student, Should use the parameters in the method header.
super(name,dob,address)

How to produce a functional LinkedList Class?

As part of an assignment I am having to produce a LinkedList class called Registry. It is intended to be part of a simple student registration system with an interface.
Unfortately, I have literally hit a wall and have no idea on what to do next with what I am doing here. The Registry class is just intended to manage a linked list of students called studentList.
Below is the current, rather incomplete class I have made so far.
import java.util.*;
public class Registry
{
LinkedList<Student> studentList;
public Registry()
{
}
public void addStudent(Student aStudent)
{
studentList.add(aStudent);
}
public void deleteStudent(int studentID)
{
studentList.remove(studentID);
}
#Override
public String toString()
{
return getClass().getName() +
}
public String format()
{
System.out.format(studentList);
}
}
Now, my main worry is using Student. As part of the assignment, I have had to make another class called Student which create instances of Students, containing forenames, surnames, Student IDs and degree Schemes as strings.
How will I be able to use that sperate class to be added to the LinkedList instanted in Registry? And how can I get the Registry class to fully function?
I will try and provide any additional details on request. I am likely unclear, so if I am, let me know, and I will try and explain as best I can.
EDIT: This is the Student Class as requested:
public class Student
{
private String foreName;
private String surName;
private String studentID;
private String degreeScheme;
public Student()
{
}
public void setForeName(String foreName)
{
this.foreName = foreName;
}
public String getForeName()
{
return foreName;
}
public void setSurName(String surName)
{
this.surName = surName;
}
public String getSurName()
{
return surName;
}
public void setStudentID(String studentID)
{
this.studentID = studentID;
}
public String getStudentID()
{
return studentID;
}
public void setDegreeScheme(String degreeScheme)
{
this.degreeScheme = degreeScheme;
}
public String getDegreeScheme()
{
return degreeScheme;
}
#Override
public String toString()
{
return getClass().getName() + "[foreName = " + foreName + " surName "
+ surName + " studentID " + studentID + " degreeScheme "
+ degreeScheme + "]";
}
public void format()
{
System.out.format("%5s%20s%11s%20s", foreName, surName, studentID, degreeScheme);
}
}
import java.util.Iterator;
import java.util.LinkedList;
public class Tester {
public static void main(String[] args) {
Registry r = new Registry();
r.addStudent(new Student("13", "John", "Doe", "Physics")); // Add a student to the Registry
r.addStudent(new Student("212", "Jane", "Bow", "Chem")); // Add another Student
System.out.println(r); // Print the Student List
r.deleteStudent(212); // Deletes student with ID 212
System.out.println(r);
}
}
class Student {
private String studentID;
private String foreName;
private String surName;
private String degreeScheme;
public Student(String studentId, String foreName, String surName, String degreeScheme) {
this.studentID = studentId;
this.foreName = foreName;
this.surName = surName;
this.degreeScheme = degreeScheme;
}
public void setForeName(String foreName) {
this.foreName = foreName;
}
public String getForeName() {
return foreName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getSurName() {
return surName;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getStudentID() {
return studentID;
}
public void setDegreeScheme(String degreeScheme) {
this.degreeScheme = degreeScheme;
}
public String getDegreeScheme() {
return degreeScheme;
}
#Override
public String toString() {
return getClass().getName() + "[foreName = " + foreName + " surName " + surName + " studentID "
+ studentID + " degreeScheme " + degreeScheme + "]";
}
public void format() {
System.out.format("%5s%20s%11s%20s", foreName, surName, studentID, degreeScheme);
}
}
class Registry {
LinkedList<Student> studentList;
public Registry() { // Creates studentList
studentList = new LinkedList<>();
}
public void addStudent(Student aStudent) {
studentList.add(aStudent);
}
public void deleteStudent(int studentID) {
int index = searchList(studentID); // Gets index of the student in the Registry
if (index == -1)
throw new IllegalArgumentException("Student not found");
// Since studentList is implemented as LinkedList, .remove removes element at specified position
studentList.remove(index);
}
// Search by studentID , if found, return position in the list
private int searchList(int studentID) {
Iterator<Student> it = studentList.iterator();
int count = -1;
while (it.hasNext()) {
count++;
Student temp;
temp = it.next();
if (Integer.parseInt(temp.getStudentID()) == studentID) {
return count;
}
}
return -1;
}
#Override
//Suggestions to improve the toString are welcome
public String toString() {
for (Student student : studentList) {
student.format();
System.out.println();
}
return "";
}
}
In any data structure there are three functions that almost always require implementation:
Insertion
Searching
Deletion
Let me begin by clarifying what the general Linked List structure consists of.
The linked list works by operating on nodes. Each node contains the actual data you want to store/modify/access.
Registry: This should be responsible for maintaining the structure and providing a way of inserting/searching for/deleting specific nodes in the list.
Student: Stores the data and controls how it is accessed and modified
So far, you have your Registry framework created appropriately. (Although you will want to implement a search method.)
For your student class, you simply create the member variables of the class that you need, as well as the appropriate getters/setters for them.
public class Student {
private String id;
private String forename;
private String surname;
private String degreeScheme;
private Student next; // Maintains a reference to the next node in the list
Student () {
//Default constructor values
}
Student (String id, String forename, String surname, String degreeScheme, Student next) {
this.id = id;
this.forename = forename;
this.surname = surname;
this.degreeScheme = degreeScheme;
this.next = next;
}
public void setID (String id) {
this.id = id;
}
public String getID () {
return id;
}
public void setforename (String forename) {
this.forename = forename;
}
public String getforename () {
return forename;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSurname () {
return surname;
}
public void setDegreeScheme(String degreeScheme) {
this.degreeScheme = degreeScheme;
}
public String getDegreeScheme () {
return degreeScheme;
}
public void setNext (Student next) {
this.next = next;
}
public Student getNext () {
return next;
}
} //End Student Class
This should be a good base to get you started. Remember, when creating data structures you'll avoid a lot of headaches by making sure that you have a clear separation of concerns. Make the student class purely responsible for storing and maintaining the data, and let the Registry class be responsible for maintaining the structure itself!
At this point, you can utilize the Student class within your Registry class, just insert the nodes as you want, search for them, delete them as needed.

Where should i declare the field in this code in order for it to compile?

This is not supposed to be a client class. I'm just making a class for others to use. I'm using this for a Highschool. For example i have classes for the address, teacher, students, principal, roomnumber, etc..But its not compiling for some odd reason. I believe its because I'm not declaring a field but not sure.
import java.io.*;
public class HighSchool {
// Constructors
public HighSchool() { }
public HighSchool(String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects ) {
this.title = title;
this.teacher = teacher;
this.roomNumber = roomNumber;
this.period = period;
this.String[] students = students;
this.String address =a ddress;
this.String subjects = subjects;
}
public class Classcourse (String title, String teacher, int roomNumber, String period, String[] students, String address, String subjects
private String period;) {
public String gettitle() {
return title;
}
public void settitle(String title) {
this.title = title;
}
public String getteacher() {
return teacher;
}
public void setteacher(String teacher) {
this.teacher = teacher;
}
public int getroomNumber() {
return roomNumber;
}
public void setroomNumber (int roomNumber) {
this.roomNumber = roomNumber;
}
public String getperiod() {
return getperiod();
}
public void setperiod (String period) {
this.period = period;
}
public String[] getstudents () {
return students[];
}
public void setstudents[] (String[] students
private String address;) {
this.students = students;
}
public String getaddress() {
return address;
}
public void setaddress (String address) {
this.address = address;
}
public String getsubjects() {
return subjects;
}
public void setsubjects (String subjects) {
this.subjects = subjects;
}
}
// modifier method
public void addstudents(String students) {
String[] newstudents = new String[students.length + 1];
for (int i = 0; i < students.length; i++) {
newstudents[i] = students[i];
}
newstudents[students.length] = student;
students = newstudents;
}
public boolean isInClass(String students) {
for (int i = 0; i < students.length; i++) {
if (students[i].equals(students)) {
return true;
}
}
return false;
}
// static creator method
public static HighSchool readFromInput() throws IOException {
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a HighSchool title: ");
HighSchool newHighSchool = new HighSchool(kb.readLine());
String students = null;
do {
System.out.print("Enter a student, or press Enter to finish: ");
students = kb.readLine();
if (students != null){
newHighSchool.addstudents(students);
}
} while (students != null);
return newHighSchool;
}
// Variables (Fields)
private String title;
private String[] students;
}
In addition, you wrote something that doesn't make sense from the point of view of Java Compiler:
private String period;) {
- probably remove ")".
The second thing:
Take a look on the declaration of class Classcourse.
It rather sounds wrong, although it can be an issue of this site's editor or something...
An "overall" hint - java has a very "intelligent" compiler in the most of the cases it can say what's wrong exactly with your code, so, assuming you're a newbie in Java, try to understand what compiler says to you.
Good luck!
Some things I noticed about the code:
public String getperiod() {
return getperiod();
}
This code will cause a endless loop when you call this function.
private String address;) {
this.students = students;
}
The compiler will give an error about the ";)". Change it to "()" to fix this.
Furthermore, you should really tell us more about the errors it's giving you. We can't help you if you don't give us the compiler errors.

Categories

Resources