So I need help with this part of JAVA in my COP class OOP programming.
First is that I need to change the addStudent to static method but the code will not run because the this.student is not static which makes no sense because it already private static
import java.util.Arrays;
public class InitializerDemo {
public static final int MAX_STUDENTS = 10;
private static Student[] students;
private Instructor instructor;
private static int numStudents = 0;
// default constructor
public InitializerDemo() {
}
// instructor mutator
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
// add a student, increment the count
//This PART!!! HELP
public static void addStudent(Student s) {
this.students[numStudents++] = s;
}
public static void main(String[] args) {
// create our aggregator object
InitializerDemo id = new InitializerDemo();
// set the instructor
id.setInstructor(new Instructor("Sally"));
// add the students
id.addStudent(new Student("Sam"));
id.addStudent(new Student("Rajiv"));
id.addStudent(new Student("Jennifer"));
id.addStudent(new Student("Test Student"));
// output
System.out.println(id);
}
public String toString() {
String s = "Instructor = " + instructor + "\n" +
"Number of students = " + numStudents + "\n" +
"Students: " + Arrays.toString(students) + "\n";
return s;
}
}
class Student {
private String name;
// instance initializer block
{
name = "noname";
}
public Student() {
}
public Student(String name) {
this.name = name;
}
public String toString() { return name; }
}
class Instructor {
private String name;
// instance initializer block
{
name = "noname";
}
public Instructor() {
}
public Instructor(String name) {
this.name = name;
}
public String toString() { return name; }
}
I need help with that addStudent Method
These are the instructions and sorry to confuse all you guys and thank you for putting time to help me
change the instance variables representing the number of students and the Student array in the aggregator object to private static variables.
• change the addStudent method in the aggregator object from an instance method to a static method
• Remove all initialization/instantiation operations from the aggregator object’s default constructor; the constructor can simple be an empty method { }
• provide a static initializer block in the aggregator object which does the following:
o initializes the number of students to 0
o instantiates the student array
o adds a single student named “Test Student” to the array using the addStudent method
Change
this.students[numStudents++] = s;
to
students[numStudents++] = s;.
I believe that should work.
You also have to initialize the students, so change
private static Student[] students;
to
private static Student[] students = new Student[MAX_STUDENTS];
Related
I am trying to return the array of students in a tester class for the Course class but I keep getting a .class expected error. I've tried to do
students[].TestCourse but that doesn't work either.
public class Course {
private String courseName;
private String[] students = new String[4];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
if (numberOfStudents == students.length) {
String [] copy = new String [students.length*2];
System.arraycopy(students,0,copy,0,students.length);
students = copy;
}
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public void dropStudent(String student) {
for (int i=0;i<students.length;i++) {
if (students[i]==student) {
students[i] = null;
}
for (i=i;i<students.length-1;i++) {
students[i] = students[i+1];
}
}
}
}
public class TestCourse {
public static void main() {
Course compScience = new Course("Computer Science");
compScience.addStudent("Jack");
compScience.addStudent("Dean");
compScience.addStudent("Leon");
compScience.dropStudent("Dean");
System.out.println("The students currently in this course are "+ students[]);
}
}
Change the line in your main method to:
System.out.println("The students currently in this course are "+ Arrays.toString(compScience.getStudents()));
and it should fire up!
All you've done is try to call the field of a class directly when you need to access it via the reference you created Course compScience = new Course("Computer Science"); ... then call it's method getStudents() as follows compScience.getStudents(). To get the contents of the array you then need to wrap this method call in Arrays.toString() as above.
i would like to run the update function automatically for every object i create. What do I have to change in my code, unfortunately it doesn't work
How can I initialize an object in my ArrayList?
:(
Creating an Arraylist and initialize with name
public class Main {
public static void main(String... args) {
Zuhoerer Maria = new Zuhoerer("Maria");
Zuhoerer Sepp = new Zuhoerer("Sepp");
Zeitansager.sagAn();
}
}
class Zuhoerer {
private String name;
private String Ansager;
Zuhoerer(String name) {
this.name = name;
}
private void setAnsager(String datumstring) {
Ansager = datumstring;
}
void update() {
setAnsager(Zeitansager.getZeit());
Zeitansager.schreibeEin(name);
System.out.println(name + " hat gerade die die Zeitansage gehört:
[Datum/Uhrzeit]: " + Ansager);
Zeitansager.trageAus(name);
}
}
class Zeitansager {
private static String datumString;
private static ArrayList<String> abonnenten;
Zeitansager(String datumString) {
Zeitansager.datumString = datumString;
abonnenten = new ArrayList<>();
}
static void schreibeEin(String name) {
abonnenten.add(name);
}
static void trageAus(String name) {
abonnenten.remove(name);
}
static void sagAn() {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.YYYY' 'HH:mm:ss");
String datum = sdf.format(new Date());
datumString = datum;
for (int i=1; i <= abonnenten.size(); i++) {
abonnenten.update();
}
}
static String getZeit() {
return datumString;
}
}
update function is not called
Your ArrayList in Zeitansager have to be of type Zuhoerer rather than String.
Then in your loop in sagAn() you need to invoke:
abonnenten.get(i).update();
instead of:
abonnenten.update();
Finally your method static void schreibeEin(String name), should actually take a Zuhoerer as an argument.
To run "update" function every time you create an object you must put a call to this function inside your constructor. Like this:
Zuhoerer(String name) {
this.name = name;
update();
}
What are you trying to do with your arraylist? Give me more details so I can try to help you.
public class studentDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("How many students are there?: ");
int numberOfstuds = scan.nextInt();
int[] nOEarray = new int[numberOfstuds];
System.out.println("\nEnter names of students up to the entered amount (" + numberOfstuds + "):");
String[] namesArray = new String[numberOfstuds];
for (int i = 0; i < numberOfstuds; i++) {
namesArray[i] = scan.next();
}
System.out.println(Arrays.toString(namesArray));
}
}
That is part of my code for letting user input array size, however I am tasked with using the header below just for a method to get the size of the array, but I have tried inserting it and keep getting different error messages such as needs body(if I put semi-colon) or "requires ';'" if I don't and when I put curly braces around the section where it gets the array size it returns errors :Syntax error, insert "[ ]" to complete Dimension
- Syntax error, insert ";" to complete BlockStatements
- Syntax error on token "create", AnnotationName expected after
this token
public static Student[] create()
Here is the Student class
public class Student {
//private data members
private String name;
private long idNUmber;
//constructor
public Student(){
name="Unassigned";
idNUmber=0;
}
//overloaded constructor
public Student(String x, long y) {
name=x;
idNUmber=y;
}
//getters
public String getName() {
return name;
}
public long getIdNUmber() {
return idNUmber;
}
//setters
public void setName(String n) {
name=n;
}
public void setIdNUmber(long i) {
idNUmber=i;
}
//override
public String toString() {
return "Name: "+getName()+"\nID number: "+getIdNUmber();
}
I am extremely new to Java so forgive me if I am not asking this question correctly. I have an assignment that is asking me:
"Test your accessors and mutators in a class called Student_Testing.
First create the Student object, then set the value using your
mutator, and then print the value to the screen. Repeat for each of
the variables. Copy and paste the entire Student_Testing class."
So I currently have a Main class that is this:
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student("Joe", 123);
int id = student1.getStudentID();
String name = student1.getName();
System.out.println("ID 1: " + id);
System.out.println("Name 1: " + name);
int id2 = student2.getStudentID();
String name2 = student2.getName();
System.out.println("ID 2: " + id2);
System.out.println("Name 2: " + name2);
}
}
And I have have a class called Student which is this:
public class Student {
private String name;
private int student_id;
private double balance;
public Student() {
name = "";
student_id = 0;
balance = 0.0;
}
public Student(String input_name, int id) {
name = input_name;
student_id = id;
}
public String getName() {
return name;
}
public int getStudentID() {
return student_id;
}
public void setStudentID(int number) {
student_id = number;
}
public void deposit(double amount) {
balance = balance + amount;
}
}
I have no clue how I am supposed to make the Student_Testing class and to create the object Student. I get errors every time.
Is the Student_Testing class created just like the other classes I have? And how am I supposed to create a new object Student when I already have a Student class in a different class?
Like I said I am a complete beginner when it come to Java, so if this could be explained in the simplest terms possible that would be great! Thanks!
public class Main {
public static void main(String[] args) {
Student_Testing.test();
}
}
public class Student_Testing {
public static void test(){
Student student1 = new Student();
Student student2 = new Student("Joe", 123);
int id = student1.getStudentID();
String name = student1.getName();
System.out.println("ID 1: " + id);
System.out.println("Name 1: " + name);
int id2 = student2.getStudentID();
String name2 = student2.getName();
System.out.println("ID 2: " + id2);
System.out.println("Name 2: " + name2);
}
}
public class Student {
//student class stuff...
}
Here we created a new class called Student_Testing. In this class, we created a static method called test(). The stuff inside the test() function is exactly the same as it was in your original code.
Note the similarity between Student_Testing and your Main class?
We can now call this test method from your main function by simply doing Student_Testing.test();
Since you are still a beginner, it's important that you ask any follow up questions.
I highly encourage you to read through this and understand it thoroughly
I am a java beginner and I am trying to get used to objects. Is there anyway that I can print out the value of a constructor in main? How can I print out the value's Kevin,20? Thanks
public class MainConstructor {
public static void main(String[] args) {
ConstructorClass emp1 = new ConstructorClass("Kevin", 20);
}
}
//Constructor Class
public class ConstructorClass {
private String name;
private int number;
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println("called");
}
}
Add a toString() method to ConstructorClass:
public String toString() {
return name + "," + number;
}
Then call this from main:
public static void main(String[] args) {
ConstructorClass emp1 = new ConstructorClass("Kevin",20);
System.out.println(emp1.toString());
}
Try using toString() in your class
public String toString() {
return this.name + "," + this.number;
}
and in your main just do emp1.toString(); to print it to your console
Constructor is basically just another method (but for the love of what is holy, never say that during interview or even to your professor) so there is nothing wrong with doing this:
public class ConstructorClass {
private String name;
private int number;
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println(name+" "+number);
}
}
But this solution is really ugly and kind of "hotfixy". Better solution would be to have constructor to only get the values and have separate method to print what you want:
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
}
void printNameAndNumber() {
System.out.println(name+" "+number);
}
And use the class like this in your main
ConstructorClass c = new ConstructorClass("John",85)
c.printNameAndNumber();
Also some people like to handle this by going through hoops and loops and overriding ToString, but that is being too overzealous and there is really no benefit in your case (or any other primitive case).
public ConstructorClass(String name, int number) {
this.name = name;
this.number = number;
System.out.println(name + "," + number);
}
If you want to properly print out those values, you should have getter methods set in your methods
Example below
public String getName(){
return name;
}
public int getNumber(){
return number;
}
Then to print those values, you should then use methods toString() and method print() to display your values
Example
public String toString(){
return getName() + " " + getNumber();
}
public void print(){
System.out.println(toString());
}
Then in the class with the main method, you call your print method for that specific class
Example
ConstructorClass emp1 = new ConstructorClass("Kevin",20);
emp1.print();
Hope this helped, Enjoy :)