# False 2 1020 3 2040 4 2521 5 2521 5 marks 8 compsci

• Notes
• UltraLightningCobra8457
• 24

This preview shows page 7 - 12 out of 24 pages.

false 2. 10,20 3. 20,40 4. 25,21 5. 25,21 (5 marks)

Subscribe to view the full document.

- 8 - COMPSCI 101 Question/Answer Sheet ID: ........................................................ CONTINUED Question 4 (13 marks) The CourseMark class is defined as follows: public class CourseMark { private String studentName; //student name private double assignmentMark; //assignment mark private double labMark; //lab mark public CourseMark(String studentName, double labMark, double assignmentMark) { this.studentName = studentName; this.assignmentMark = assignmentMark; this.labMark = labMark; } public void setAssignmentMark(double assignmentMark) { this.assignmentMark = assignmentMark; } public double calculateTotal(){ return assignmentMark + labMark; } public String toString() { return "Name: " + studentName + "\n assignments: " + assignmentMark + "\n labs: " + labMark + "\n coursework total: " + calculateTotal(); } } a) Write a Java statement (or statements) which creates an instance of the CourseMark class, called “myCourseMark”, so that the instance has the state visualised as: CourseMark myCourseMark = new CourseMark("Karen",10.0, 20.5);
- 9 - COMPSCI 101 Question/Answer Sheet ID: ........................................................ CONTINUED (3 marks) b) Based on your answer in part (a) above, what is the output when the following code is executed? myCourseMark.setAssignmentMark(21.0); System.out.println(myCourseMark); Name: Karen assignments: 21.0 labs: 10.0 coursework total: 31.0 (5 marks) c) Assume two CourseMark objects are equal in value if their coursework totals (i.e. the sum of the assignment mark and the lab mark) are equal. Define the equals() method for the CourseMark class which returns a boolean indicating whether the current CourseMark object equals another CourseMark object in value. public boolean equals( CourseMark other ) { return other.calculateTotal() == calculateTotal(); OR return Math. abs (other.calculateTotal() - calculateTotal())<0.001; } (5 marks)

Subscribe to view the full document.

- 10 - COMPSCI 101 Question/Answer Sheet ID: ........................................................ CONTINUED Question 5 (17 marks) The Music class is defined as follows: public class Music { private String singer; //name of the singer private String song; //name of the song private String type; //type of the music, //e.g. pop, classical, jazz public Music(String singer, String song, String type) { this.singer = singer; this.song = song; this.type = type; } public String getSinger() { return singer; } public String getSong() { return song; } public String getType() { return type; } public String toString() { return singer + ": " + song + " (" + type +")"; } } a) Write a Java statement which declares an array of Music objects. Use the identifier myMusicCollection for the array variable. Music[] myMusicCollection; (2 marks)
- 11 - COMPSCI 101 Question/Answer Sheet ID: ........................................................ CONTINUED b) Write a Java statement which constructs the myMusicCollection array (that you declared in part (a) above) to be large enough to store exactly 500 Music objects. myMusicCollection = new Music[500]; (2 marks) c) Write a Java statement which stores the object, myFavourite , in index position 0 of the myMusicCollection array. The myFavourite variable has been declared and initialised for you. Music myFavourite = new Music("Andrea Bocelli", "Besame Mucho", "pop"); myMusicCollection[0] = myFavourite; (2 marks) For parts (d) to (f) below, assume that the myMusicCollection array has been created and contains some Music objects.

Subscribe to view the full document.

You've reached the end of this preview.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern