It's UWAweek 18 (1st semester, week 9)

help2005

This forum is provided to promote discussion amongst students enrolled in CITS2005 Object Oriented Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying selected article
Showing 1 of 112 articles.
Currently 1 other person reading this forum.


 UWA week 17 (1st semester, week 8) ↓
SVG not supported

Login to reply

👍?
helpful
12:49pm Wed 24th Apr, Andrew G.

While your notes are mostly correct, they do contain a number of errors. I am probably not going to be able to address them all here, but I have already explained the answers in a lecture and a PDF of the answers is available on LMS under the Mid-Semester Test Info page. I will the ones I can pick out, though, in an effort to prevent this causing yourself and other students future misunderstandings.
> multiple answers to one question that takes at least 1 min to trace for each value then a question that is only supposed to take 1 min tun into 4-5mins to check each answer
I assume this is referring to Question 2, as I can see no reason to trace any other question multiple times. There is no need to trace every option, as you can analyse the code to reason about what properties an input must have to return true, which then allows you to pick the answer easily. Even if you do not make this observation, the method in question is three if statements in sequence, and should not take the typical student more than a few seconds to trace. This was specifically chosen so as to allow tracing as a method in the available time. Question 2 should certainly not be taking you 4-5 minutes.
> *** ok, the next one genuinely doesn't make sense because if you actually trace it will cause NullPointerExeption and not compile *** > > Q7:What does the above Java program print? (Select the single correct answer) > a) 2 > b) 3 > c) 4 > d) 5 ✓ > e) 6 > f) It does not compile successfully > g) It throws an exception at runtime > > if we trace this correctly: > > 1. Mystery a = new Mystery(); creates a new Mystery object named a with count initialized to 0. > > 2. Mystery b = a; creates another reference b that points to the same Mystery object as a. At this point, a.count and b.count both reference the same count which is 0. > > 3. Mystery c = new Mystery(a, 3); invokes the constructor that increases a.count (and therefore b.count because a and b are references to the same object) by 3. Now, a.count and b.count are both 3. > > 4. Mystery d = new Mystery(b); effectively calls new Mystery(b, 1); because of the this(x, 1); call in the second constructor. This increases b.count by 1 (which is also a.count since a and b are still the same object). Now, a.count and b.count are both 4. > > 5. a = null; sets the reference a to null, but this doesn't affect the object itself. b is still referencing the original object with count 4. > > 6. a.count += 1; would throw a NullPointerException because a is now null. However, if we assume this line is not there, we can match the expected answer of 5. > > 7. The System.out.println(b.count); would print b.count, which is currently 4. But if we assume the increment from the non-existent line a.count += 1; then the count would be 5. > > > But with the code as written, the correct answer is g) It throws an exception at runtime.
Your tracing is incorrect. You have swapped the lines `a = null;` and `a.count += 1;`. If in doubt you can attempt to compile and run the code, it is a complete Java source file:
> javac Mystery.java > java Mystery
5 Which gives 5, as expected. I was careful to test the code used in the mid-sem ahead of time to make sure it did not contain any typos or other minor errors that could lead to confusion.
> public class Mystery { > private int counter = 0; > > public void tick() { > counter++; > } > > public int get() { > return counter; > } > > public static void main(String[] args) { > Mystery.tick(); > Mystery.tick(); > System.out.println(Mystery.get()); > } > } > > > Q9:What does the above Java program print? (Select the single correct answer) > > a) 0 > b) 1 > c) 2 > d) The program does not compile ✓ > e) The program throws a runtime error. > > Does not compile, Attempting to access a field on a null reference is invalid
You are correct that it does not compile, but your reasoning is incorrect. There is no null reference in this code to attempt to access a member from. This code does not compile because `Mystery` is a class and hence `Mystery.tick()` is an attempt to call a static method on that class, but no such static method exists, as `tick()` is non-static. To call `tick()`, one would first have to construct an instance of a `Mystery` object, and then call the `tick()` method on that object.
> Q10: Which of the following would be reasonable as an example of a subclass relationship? (There may be multiple correct answers. Select all of them.) > > a) Apple is a subclass of Fruit ✓ (it's a type of fruit) > b) Vehicle is a subclass of Truck > c) Aeroplane is a subclass of Vehicle ✓ (it's a type of vehicle) > d) Apple is a subclass of Vehicle > e) Wheel is a subclass of Truck (Goz said no, but in reality it's a type of part of a car so if the class was car_parts() then yes, but for this case no. The classes have to be able to be interchangeable a=b and b=a)
I am not sure what you mean by a class `car_parts()`. If you mean to propose the existence of a class, let's say `CarPart` that represents any part of a car, then yes, it would be plausible that `Wheel` is a subclass of `CarPart`. But the question asks for whether Wheel is a subclass of Truck. Recall that the property of a subclass is that it inherits properties from its parent class, and so can be used anywhere its parent could be. As you put it, A is a subclass of B if A is a *type* of B. So Wheel is not a subclass of Truck, as a wheel does not have all the properties of a Truck, and cannot be used as a Truck. You also say that "the classes have to be able to be interchangeable a=b and b=a". This is not correct. There is no requirement for the superclass to be able to be substituted for the subclass. The subclass (Apple, for example) may have properties its superclass (Fruit) does not, and so logic that works with Apples may not be able to work with Fruits, as it may depend specifically on the properties of Apple, which Fruit may not have. The other way around is fine because if Apple is a subclass of Fruit then we know it has inherited all its properties, and so any logic that works with Fruits could work with Apples.
> Q13: Fill in the body of the following method such that it returns true if and only if num is between 1401 and 2005, inclusive. > > two most possible answers to this are > > > public static boolean isInRange(int num) { > return (1401 <= num && num <= 2005) ? true : false; > } > > > OR using if else was also valid [Goz said it was unessesary code, but if you did exactly as bellow then your answer is valid, will compile, uses return which was the main concern with the question and therefore you should get full marks for this, ask your lab facilitator that you're assigned to because they are most likely and usually the person who marks your test, ask for a review of your test + remark if you know you did it > > public static boolean isInRange(int num) { > if (1401 <= num && num <= 2005) { > return true; > } else { > return false; > } > } > > or this > > public static boolean isInRange(int num) { > boolean isGreaterThanOrEqualTo1401 = num >= 1401; > boolean isLessThanOrEqualTo2005 = num <= 2005; > return isGreaterThanOrEqualTo1401 && isLessThanOrEqualTo2005; > }
These are all valid answers that would receive full marks, despite being unnecessarily over-complicated, because no marks were awarded for simplicity. So long as you wrote valid Java that implemented the correct logic, you are guaranteed both marks. However, there is a simpler answer (that I gave in the sample answers) that you have omitted: public static boolean isInRange(int num) { return 1401 <= num && num <= 2005; } There is no need to use the ternary expression, as the condition you are using is already the desired result.
> FINALLY THIS COULD ALSO BE: (if you did python you would've been exposed to this) > > public static boolean isInRange(int num) { > return switch (num) { > case 1401, 1402, ..., 2005 -> true; > default -> false; > }; > }
Recall that in this unit we use the (admittedly older) Java 11. The code you have given is not valid Java 11. Your code appears to use a feature introduced in Java 12 (JEP 354), but the `...` is not valid syntax. If you mean to indicate that you could write out the full list of integers from 1401 to 2005, that would be valid Java 12 and would give the correct answer, but I would advise students not to attempt to write a list of 605 four digit numbers in the time available in a mid-sem. If someone did do that in the test, I would probably be lenient and award the marks, despite the fact that we only officially cover features that exist in Java 11.
> I hope you can see or maybe put into perspective how the last few questions should be weighted at least 3-4 marks each as they have distinctive parts that make them whole.
Questions are not necessarily weighted by difficulty. If you are able to do the more difficult problems, you are already solving more problems than those who can only do the easier ones. Each of the written questions is designed to assess your working understanding of how to do different things in Java. Each question builds upon the previous. To get the marks for Question 13 you only need to construct and return a simple expression. To get the marks for Question 14 you must now introduce control structures such as loops and if statements, while working with arrays. To get the marks for Question 15 you only need to produce an extremely simple class that just stores two fields and does no logic with them. To get the marks for Question 16 you must now demonstrate doing simple computations with the fields of a class. The different capabilities being assessed in each question are not so challenging as to be deserving of more marks.
> [----------------------------------[end of my rant]-------------------------------------]
Your notes for most of the questions were well done. I hope the above helps with the mistakes that were present. I will end with a reminder that the mid-semester test is a formative assessment, where the goal is to highlight what material students are comfortable with and what material they may have misunderstood and need to revise. I encourage all students to explore the questions where they lost marks in greater detail and attempt to reinforce their understanding of the concepts at play in those questions. For those students who got full marks, this hopefully indicates you have a good broad understanding of the material we have covered so far in this unit, though it is of course impossible to assess everything in a single test.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy