Currently I have two classes. A Classroom class and a School class. I would like to write a method in the School class public void showClassRoomDetails which would find the classroom details by only using the teacherName.
e.g.
teacherName = Daniel className = Science
teacherName = Bob className = Maths
so when I input Bob, it would print out Bob and Maths
many, thanks
public class Classroom
{
private String classRoomName;
private String teacherName;
public void setClassRoomName(String newClassRoomName)
{
classRoomName = newClassRoomName;
}
public String returnClassRoomName()
{
return classRoomName;
}
public void setTeacherName(String newTeacherName)
{
teacherName = newTeacherName;
}
public String returnTeacherName()
{
return teacherName;
}
}
import java.util.ArrayList;
public class School
{
private ArrayList<Classroom> classrooms;
private String classRoomName;
private String teacherName;
public School()
{
classrooms = new ArrayList<Classroom>();
}
public void addClassRoom(Classroom newClassRoom, String theClassRoomName)
{
classrooms.add(newClassRoom);
classRoomName = theClassRoomName;
}
public void addTeacherToClassRoom(int classroomId, String TeacherName)
{
if (classroomId < classrooms.size() ) {
classrooms.get(classroomId).setTeacherName(TeacherName);
}
}
public void showClassRoomDetails
{
//loop
System.out.println(returnClassRoomName);
System.out.println(returnTeacherName);
}
}
Do you really need a list here ?
A Map holding classroom - teacher associations would be more helpful for what you're trying to achieve.
But there are also strange things in your code: for instance, why do you hold a classRoomName and teacherName as instance variables in your School class ?
Change your method signature to take a targetTeacherName as a parameter. Loop through the classrooms until you find one with that teacher. Output the information of that classroom.
Since you are using an ArrayList you could simply use a for each statement:
public void showClassRoomDetails(String teacherName)
{
for (Classroom classroom : this.classrooms)
{
if (classroom.returnTeacherName().equals(teacherName))
{
System.out.println(classroom.returnClassRoomName());
System.out.println(classroom.returnTeacherName());
break;
}
}
}
As a small suggestion, do not name your methods returnXxx(), use getXxx() instead, it is the standard Javabean convention.
Related
How does one print the private int data field of an object that's within another object's array.
So if I had an object called Classroom and another object called Student, how would I print the student ID's of the student objects inside the Classroom object's private array member?
Would I override toString within Student to print the studentID? But how do you use that in the Classroom object's array to print out the array of IDs?
In your Student class, you should create a method that returns the student's id, like in the example below:
class Student
{
private id;
//... constructor and other code
int getID() {return this.id;}
}
In your Classroom class, you should create a method that adds the student to the array of students (I used an ArrayList in this case) and a method that prints the ids of all students in the list. Look below:
class Classroom
{
private ArrayList<Student> studentsList;
//... constructor and other code
void addStudent(Student student) {
this.studentsList.add(student);
}
void printStudentsList() {
for(Student student: this.studentsList) {
System.out.println(student.getID());
}
}
}
Note that it's just one of the ways you can use to achieve what you want. Since you didn't post your code, I improvised with the information you gave.
class Student{
private ArrayListids;
public Student(){
ids.add("S001");
ids.add("S002");
}
public ArrayList<String> getID(){
return this.ids;
}
}
class ClassRoom {
public static void main(String args[]){
Student s=new Student();
ArrayList<String>studentID=s.getID();
for(String id:studentID){
System.out.println("Student ID :"+id);
}
}
}
I think that you need to create some public methods to use the private attributes. I think you can design Student and Classroom class as following:
class Student
{
private int studentID;
//and others private attributes
public student()
{
//write something here to initiate new object
}
public int getID()
{
return studentID;
}
//you can insert others methods here
}
class Classroom
{
private Student[] studentArray;
//add constructer here if you need it
public int getStudentId(int position) //get student ID at `position` in the array
{
return studentArray[position].getID();
}
}
public class Main
{
public static void main(String[]args)
{
Classroom classroom = new Classroom();
//do something to insert student to array of the `classroom`
//assume you need to get ID of student in 6th place
System.out.println(classroom.getStudentID(6));
}
}
I think I have got myself into a bit of muddle. I had teacher and student below in two separate addMember sections. For the purpose of what I am doing they have to in one section but when someone wants to add either student or teacher they need to be able to be added separately (so one call which will give the option of adding either). But my knowledge of arrayLists is not very good and as you can see isn't working very well. Any help be much needed and appreciated.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Committee {
private String name;
private List<Object> members;
public Committee(String name)
{
this.name = name;
members = new ArrayList<Object>();
List list = new ArrayList();
}
public void addMember(Student student, Teacher teacher)
{
List members1 = new ArrayList();
members1.add(student);
System.out.println(members1);
List members2 = new ArrayList();
members2.add(teacher);
System.out.println(members2);
}
public void printMembership()
{
System.out.println("Membership of the " + name + " Committee ");
Iterator<Object> it = members.iterator();
while (it.hasNext())
{
Object member = it.next();
System.out.println(members);
}
}
}
I'm not sure what you want to achieve, so I will write two solutions:
Situation where you want to have one list
interface Member {...}
class Student implements Member {...}
class Teacher implements Member {...}
public class Comittee {
private list<Member> members;
public void addMember(Member member) {
members.add(member);
}
public void printMembership() {
members.forEach(System.out::println);
}
}
Situation where you want to have two lists
interface Member ...
class Student implements Member ...
class Teacher implements Member ...
public class Comittee {
private list<Teacher> teachers;
private list<Student> students;
public void addMember(Student student, Teacher teacher) {
students.add(student);
teachers.add(teacher);
}
public void printMembership() {
getAllMembers().forEach(System.out::println);
}
private void getAllMembers(): List<Member> {
List<Member> allMembers = ArrayList<>(students.size() + teachers.size());
allMembers.addAll(teachers);
allMembers.addAll(students);
return allMembers;
}
}
I have a Student class that contains an ArrayList of type Course, and Course is class with some fields like className, classTime, etc along with the appropriate getters and setters. Say I created an ArrayList of Course and have stored it into the Student class.
How can I for example print the className of a particular Course object (which is stored in an ArrayList stored in the Student class)?
So far I tried this, below is part of the code for class Student:
class Student {
ArrayList<Course> studentSchedule;
public ArrayList<Course> getStudentSchedule() {
return studentSchedule;
}
public void setStudentSchedule(ArrayList<Course> studentSchedule) {
this.studentSchedule = studentSchedule;
}
}
Then I have some code that created student1 of type Student and stored an ArrayList of Course into it.
Say I want to access the className in the first object in the ArrayList that's in student1. So far I have this and it works... is it fine?
ArrayList<Course> schedule = student1.getStudentSchedule();
System.out.print("\n course name at position 0 is " +
student1.getStudentScheduleClassName(0));
It feels weird to create another Arraylist just for this purpose... but then I thought since in line 1, schedule will only contain the addresses that point to the location and shouldn't take much space?
Is there a more appropriate way to do this?
Based on above discussion I tried to complete the solution for my reference.
import java.util.ArrayList;
import java.util.Iterator;
public class ListExample {
public static void main(String[] args) {
Student student1 = new Student();
ArrayList<Course> student1Schedule = new ArrayList<Course>();
student1Schedule.add(new Course("Computer Science", "Training Room"));
student1Schedule.add(new Course("Mobile App Development", "Training Room 2"));
student1.setStudentSchedule(student1Schedule);
// Prints only one course
System.out.println(" ** Course" + student1.getStudentSchedule().get(0).toString());
// Print all the courses attended by the student
ArrayList<Course> studentDetails = student1.getStudentSchedule();
Iterator<Course> studentIterator = studentDetails.iterator();
while (studentIterator.hasNext()) {
Course courseName = studentIterator.next();
System.out.println(courseName);
}
}
static class Student {
private ArrayList<Course> studentSchedule;
public ArrayList<Course> getStudentSchedule() {
return studentSchedule;
}
public void setStudentSchedule(ArrayList<Course> studentSchedule) {
this.studentSchedule = studentSchedule;
}
}
static class Course {
private String courseName;
private String className;
public Course (String courseName, String className){
this.className =className;
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String toString (){
return "Course Name :" + this.courseName + "\n" + "Class Name : " + className + "\n";
}
}
}
public class Course{
private String className;
public String getClassName(){
return className;
}
public void setClassName(String c){
className =c;
}
}
So when you have this you can simply do
System.out.println(student1.getStudentSchedule().get(0).getClassName())
Make className into an instance variable of Course and have getter and setter methods within the Course for the class name. Then you will print out the class name.
You can access the first course's class name as:
System.out.print("\n course name at position 0 is " + schedule.get(0).getClassName());
since you've already defined getter/setter in the Course class as you mentioned.
I have 2 class diagrams, class Address
+forename
+surename
+street
+houseno
+code
+state
+toString
second Addressbook
insert(address: Address)
toString()
searchSurename (surename: string): Address[*]
+searchForename(forename: string): Address[*]
i implemented address:
public class Address {
public static String forename;
public static String surename;
public static String street;
public static int houseno;
public static int code;
public static String state;
public String toString(){
return this.forename + this.surename + this.street + this.houseno + this.code + this.state;
}
How can I implement Addressbook as easy as possible?
EDIT:
public class addressbook{
private static ArrayList<Address> book;
public addressbook(){
book = new ArrayList<Address>();
}
}
EDIT QUESTION:
Am I allowed to add new methods or attributes in a implementation outside the ones that we use in our class diagrams?
EDIT 2:
First try implementing method searchSurename with an ArrayList:
public static String searchSurename(String surename){
boolean exist = false;
if(this.addresses.isEmpty()){
return null;
}
for(int i=0;i<this.addresses.size();i++) {
if(this.addresses.get(i).getSurename() == surename) {
exist=true;
break;
}
if(exist) {
return this.addresses.get(surename);
} else {
return this.addresses.get(surename);
}
}
// return ?!?
}
The Program give me Errors at "this" at any line, maybe a mistake but I cant tell! It Looks a Little bit too difficult, I don't find any implementations where searching through a list is simple.
You could implement it in a way like this. Look at the api for arrayList for using its methods.
public class Adressbook {
List<Adress> adresses = new ArrayList<Adress>();
public Adressbook(){
adresses = new arraylist<Adress>();
}
public insert (Adress adress){
adresses.add(adress)
}
public searchSurename(String Surename){
}
public searchForename(String forename){
}
public String toString(){
}
ArrayList api:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
To have unique address use set collection interface
public class Adressbook {
....
private Set<Adress> adresses = null;
public Adressbook(){
adresses = new HashSet<Adress>();
}
public void add(Adress adress){
adresses.add(adress)
}
...
}
Currently I have two classes. a classroom class and a School class. I would like to write a method in the School class to call public void setTeacherName(String newTeacherName) from the classroom class.
classroom.java
public class classroom {
private String classRoomName;
private String teacherName;
public void setClassRoomName(String newClassRoomName) {
classRoomName = newClassRoomName;
}
public String returnClassRoomName() {
return classRoomName;
}
public void setTeacherName(String newTeacherName) {
teacherName = newTeacherName;
}
public String returnTeacherName() {
return teacherName;
}
}
School.java
import java.util.ArrayList;
public class School {
private ArrayList<classroom> classrooms;
private String classRoomName;
private String teacherName;
public School() {
classrooms = new ArrayList<classroom>();
}
public void addClassRoom(classroom newClassRoom, String theClassRoomName) {
classrooms.add(newClassRoom);
classRoomName = theClassRoomName;
}
// how to write a method to add a teacher to the classroom by using the
// classroom parameter
// and the teachers name
}
You should capitalize names of your classes. After doing that do this in your school class,
Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);
Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. Ex: right click Source -> Generate getters and setters
Try this :
public void addTeacherToClassRoom(classroom myClassRoom, String TeacherName)
{
myClassRoom.setTeacherName(TeacherName);
}
class A{
public void methodA(){
new B().methodB();
//or
B.methodB1();
}
}
class B{
//instance method
public void methodB(){
}
//static method
public static void methodB1(){
}
}
in School,
public void addTeacherName(classroom classroom, String teacherName) {
classroom.setTeacherName(teacherName);
}
BTW, use Pascal Case for class names. Also, I would suggest a Map<String, classroom> to map a classroom name to a classroom.
Then, if you use my suggestion, this would work
public void addTeacherName(String className, String teacherName) {
classrooms.get(className).setTeacherName(teacherName);
}
Instead of using this in your current class setClassRoomName("aClassName"); you have to use classroom.setClassRoomName("aClassName");
You have to add the class' and at a point like
yourClassNameWhereTheMethodIs.theMethodsName();
I know it's a really late answer but if someone starts learning Java and randomly sees this post he knows what to do.