E41e6fdad12edcfb97ab662e83bfb9ab

Having some trouble with the methods, I've included some comments so you can see what I tried or intented to achieve here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class SchoolRoom {
    private String [] students;
    
    public SchoolRoom(String [] students) {
        this.students = students;
    }

    // Returns the number of students in this SchoolRoom.                                       
    public int enrolment() {
       return students; 
    } 
 
    /**
     * Returns the index of the specified student in this SchoolRoom.
     * If the student appears more than once, only the index
     * of the first one (counting from the left) is returned. If the
     * student is not found in the array, -1 is returned.
     * if it is found. Returns -1 if the student is not in the array.
     */
     public int indexOf(String target) {
       

    }

    /**
     * Compares this SchoolRoom object to the object passed
     * to the method. If that object is also an instance of
     * SchoolRoom, has the same enrolment (number of students), and 
     * every Student's name in that object is the same as the name 
     * of every Student in the same array position in this
     * SchoolRoom, then the objects are equal.
     */
    public boolean equals(Object otherString) {

        SchoolRoom tmpObj = (SchoolRoom) o;

        if (!(o instanceof SchoolRoom))
            return false;

        //return (;

    }

    /**
     * Returns a new SchoolRoom which has the same students
     * as this SchoolRoom, but in reverse order. 
     */
    public SchoolRoom reverse() {

 
    }
 
    /**
     * Returns a new SchoolRoom which has all-capitals names corresponding
     * to the students in this Schoolroom. 
     */
     public SchoolRoom capitalize() {
        String SchoolRoom;

        System.out.println(SchoolRoom.toUpperCase()); 
     }
}



   

Refactorings

No refactoring yet !

3c02900ee6d9b02a85753fd4ef04a10c

Pierre Joubert

November 6, 2007, November 06, 2007 08:13, permalink

No rating. Login to rate!
1
2
3
4
    // Returns the number of students in this SchoolRoom.                                       
    public int enrolment() {
       return students.length; //the .length property on an array contains the number of elements in the array 
    } 
97a5f192af6874da69c9e6e6554cd5ad

Gregor Samsa

November 6, 2007, November 06, 2007 11:25, permalink

1 rating. Login to rate!

Ah, cs101, the memories!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public class SchoolRoom
{
	private String [] students;
    
	public SchoolRoom(String [] students)
	{
	   this.students = students;
	}

    // Returns the number of students in this SchoolRoom.                                       
	public int enrolment()
	{
		return students.length; 
	} 
 
    /**
     * Returns the index of the specified student in this SchoolRoom.
     * If the student appears more than once, only the index
     * of the first one (counting from the left) is returned. If the
     * student is not found in the array, -1 is returned.
     * if it is found. Returns -1 if the student is not in the array.
     */
  public int indexOf(String target)
	{
     for(int i=0;i<students.length;i++)
		 {
		    if(students[i].equals(target))
					return i;
		 }
		 return -1;
	}

    /**
     * Compares this SchoolRoom object to the object passed
     * to the method. If that object is also an instance of
     * SchoolRoom, has the same enrolment (number of students), and 
     * every Student's name in that object is the same as the name 
     * of every Student in the same array position in this
     * SchoolRoom, then the objects are equal.
     */
    public boolean equals(Object otherString)
		{

        SchoolRoom tmpObj = (SchoolRoom) otherString;
				
				if(tmpObj.enrolment()!=this.enrolment())
				{
					return false;
				}
			
				for(int i=0; i<this.enrolment();i++)
				{
					if(!(this.getStudent(i).equals(tmpObj.getStudent(i))))
					{
						return false;
					}
				}
				return true;
			}
    /**
     * Returns a new SchoolRoom which has the same students
     * as this SchoolRoom, but in reverse order. 
     */
    public SchoolRoom reverse()
		{
			String[] tmp = new String[this.students.length];
			for(int i=0;i<this.students.length;i++)
			{
				tmp[i] = this.students[this.students.length-i-1];
			}
			
			return new SchoolRoom(tmp);
    }
 
    /**
     * Returns a new SchoolRoom which has all-capitals names corresponding
     * to the students in this Schoolroom. 
     */
     public SchoolRoom capitalize()
		 {
			 String[] tmp = new String[this.students.length];
			 for(int i=0;i<this.students.length;i++)
			 {
			  	tmp[i] = this.students[i].toUpperCase();
			 }
			
			 return new SchoolRoom(tmp);
     }
		 
		 /**
		 * Returns the Student at the given position
		 *
		 */
		 public String getStudent(int i)
		 {
				try
				{
			     return this.students[i];
		    }
				catch(Exception e)
				{
					return "None found.";
				}					
		 }
		 
		 /**
		 * Tester
		 *
		 */
		 public static void main(String[] args)
		 {
			 System.out.println("Tester for SchoolRoom class");
			 String[] t1 = {"Gregor Samsa", "James Joyce", "Brendan Behan"};
			 String[] t2 = {"Brendan Behan", "James Joyce","Gregor Samsa"};
			 SchoolRoom test1 = new SchoolRoom(t1);
			 SchoolRoom test2 = new SchoolRoom(t2);
			 
			 System.out.println(test1.capitalize().reverse().indexOf("JAMES JOYCE"));
			 System.out.println(test1.capitalize().reverse().getStudent(0)+ ", " +
			 test1.capitalize().reverse().getStudent(1)+ ", "+
			 test1.capitalize().reverse().getStudent(2));
			 
			 System.out.println(test1.equals(test2.reverse()));
		 }
}
E41e6fdad12edcfb97ab662e83bfb9ab

Mark Webb

November 6, 2007, November 06, 2007 12:26, permalink

No rating. Login to rate!

Many thanks

017ec70e269e6f69ed8979f8b41bb5d3

fenix

November 7, 2007, November 07, 2007 14:02, permalink

1 rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    /**
     * Compares this SchoolRoom object to the object passed
     * to the method. If that object is also an instance of
     * SchoolRoom, has the same enrolment (number of students), and 
     * every Student's name in that object is the same as the name 
     * of every Student in the same array position in this
     * SchoolRoom, then the objects are equal.
     */
	public boolean equals(Object obj) {
	    if (obj != null && obj instanceof SchoolRoom) {
	        SchoolRoom schoolRoom = (SchoolRoom)obj;
	        if (this.students != null && this.enrolment() == schoolRoom.enrolment()) {
	            for (int i = 0; i < this.enrolment(); i++) {
	                if(!(this.getStudent(i).equals(schoolRoom.getStudent(i)))) {
	                    return false;
	                }
	            }
	            return true;
	        }
	    }
	    return false;
	}
7a57bc1b767829f7fa9673f4cd04298d

hadrien

November 7, 2007, November 07, 2007 16:49, permalink

No rating. Login to rate!

I would use a Vector to back the array.
Did not run my code and it is not 100% safe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;

public class SchoolRoom {
	private Vector<String> students;

	public SchoolRoom() {
		students = new Vector<String>();
	}
	
	@SuppressWarnings("unchecked")
	protected SchoolRoom(Vector<String> students) {
		this.students = Vector.class.cast(students.clone());
	}

	public SchoolRoom(String[] students) {
		this.students = new Vector<String>(students.length);
		for (String str : students)
			this.students.add(str);
	}

	/*
	 *  Returns the number of students in this SchoolRoom.
	 */
	public int enrolment() {
		return students.size();
	}

	/**
	 * Returns the index of the specified student in this SchoolRoom.
	 * If the student appears more than once, only the index
	 * of the first one (counting from the left) is returned. If the
	 * student is not found in the array, -1 is returned.
	 * if it is found. Returns -1 if the student is not in the array.
	 */
	public int indexOf(String target) {
		return this.students.indexOf(target);
	}

	/**
	 * Compares this SchoolRoom object to the object passed
	 * to the method. If that object is also an instance of
	 * SchoolRoom, has the same enrolment (number of students), and 
	 * every Student's name in that object is the same as the name 
	 * of every Student in the same array position in this
	 * SchoolRoom, then the objects are equal.
	 */
	public boolean equals(Object obj)
	{
		try{
			SchoolRoom cmp = SchoolRoom.class.cast(obj);
			return cmp.students.equals(this.students);
		}catch(ClassCastException e)
		{
			return false;
		}
	}

	/**
	 * Returns a new SchoolRoom which has the same students
	 * as this SchoolRoom, but in reverse order. 
	 */
	public SchoolRoom reverse() {
		SchoolRoom newObj = new SchoolRoom(this.students);
		Collections.reverse(newObj.students);
		return newObj;
	}

	/**
	 * Returns a new SchoolRoom which has all-capitals names corresponding
	 * to the students in this Schoolroom. 
	 */
	public SchoolRoom capitalize() {
		Vector<String> newObj = new Vector<String>(students.size());
		
		for( String str : this.students )
			newObj.add(str.toUpperCase());
		
		return new SchoolRoom(newObj);
	}
}
Avatar

anton

November 22, 2007, November 22, 2007 01:29, permalink

No rating. Login to rate!

this is homework

Your refactoring





Format Copy from initial code

or Cancel