XML

COMP3200 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for assign2

2024

Question 56 (2024):

Submission reference: IN3701

for the removeType method, would it be advisable to use a for loop or a while loop?

Answer 56:

I am not sure if you mean a 'for loop' or a 'foreach loop'. You cannot use a for-each loop for that method. You could use a for loop, but we haven't taught for loops yet, so you would need to check your code carefully to make sure it is correct. So, the best choice would be a while loop.

Keywords: assign2 , method-removeType


Question 54 (2024):

Submission reference: IN3699

hello, for assignment 2, once we input both the tests, if theres no errors in the tests does that mean our code is correct?

Answer 54:

It's not an absolute guarantee, but it's a good indication.

Keywords: assign2


Question 53 (2024):

Submission reference: IN3698

hi prof , do we need to do anything on the main class or do i just leave it as it it(empty)

Answer 53:

You don't have to do anything with the Main class. If you are using the JUnit test classes to check your code, then you can leave Main empty. If you aren't using the test classes then you will probably want to write your own testing code in main as we demonstrated in last week's lectures. But we won't be marking you on any code in the Main class.

Keywords: assign2


Question 52 (2024):

Submission reference: IN3697

hi, I have tried to find the mistake for hours and I still dont understand what is wrong because the error that comes up says I am comparing 2 strings with an '==' but I am just reassigning the variable so when, it is displayed it has an X next to the number, but it seems to not work. Here is my code:

    public void setType(String name, String type) {
	 code deleted
		String updatedType = restaurant.getFoodType() + "X";
		return ;
         code deleted
	    }
Thank you.

Answer 52:

If you want to change the type of a Restaurant object then you have to call the type setter method on it. At the moment, you are just getting the food type and changing the local variable, so you are not mutating the Restaurant object at all.

Keywords: assign2 , method-setType


Question 46 (2024):

Submission reference: IN3691

hi, I have one error left to fix but I'm unsure where to start

code deleted
testFindRestaurantMultipleClosestHighLow()

Answer 46:

You haven't fully implemented this requirement:

If more than one restaurant is of the target type and nearest to the given price, then the cheapest must be returned.

Keywords: assign2 , method-findBestPrice


Question 45 (2024):

Submission reference: IN3690

Hello, I have two errors and I sort of understand why they are appearing but I don't know how to fix them. Code:

code deleted
Errors: testFindRestaurantLowest() testFindRestaurantMultipleClosestHighLow() I know that I need to change the closest price before I check it with the if statement (as it will always stay at 0) but I'm unsure of how to implement it.

Answer 45:

Having a price difference of zero as your starting point won't work, because you can never find a better price difference among the restaurants. So, a good place to start would be to use an initial value that is higher than any possible price difference.

Also, overwriting bestChoice before you have checked the price difference will always change the selected restaurant regardless of its price.

Keywords: assign2 , method-findBestPrice


Question 44 (2024):

Submission reference: IN3689

code deleted
This is a copy and paste of my code in the RestaurantSelection class. I'm very confused on how to fix the error at "restaurants.add(restaurants);" I tried the suggestions and I just get more error messages. How do I fix this?

Answer 44:

You are trying to add the list (restaurants) to the list when you should be adding the Restaurant parameter (restaurant) to the list. By the way, you have mis-spelled restaurants.

Keywords: assign2


Question 40 (2024):

Submission reference: IN3685

Hello David, after finishing the first bit of Assignment 2 restaurant. Should we create Restaurant Selector in a different class in the same project or should we start a different one altogether?

Answer 40:

The skeleton code we have provided contains an outline RestaurantSelector class. Complete the code in that class. The links to access the code are given in the assignment brief on the first page.

Keywords: assign2


Question 39 (2024):

Submission reference: IN3684

Hi Daivd, im currently got one error in the test class, i realise its to do with my code only taking the first element of the array that is the smallest and not going with the one that is the actual smallest. Im not sure where to go next though. Thanks

code deleted

Answer 39:

I think you have to recognise that your choice of a for-each loop means that you intend to examine every restaurant in the list. However, you are subverting that by returning from inside the body of the loop. You should not use a return statement inside a for-each loop. You can only be sure that you have found the right restaurant once the loop has fully completed. So, you need to find a way to remember that you have found a possibility but that you want to keep looking in case of something better later on in the list.

Keywords: assign2 , method-findBestPrice


Question 38 (2024):

Submission reference: IN3683

Hello, David I'm having issues with my error keeps coming up and I don't know why could you help me with it please?

code deleted

Answer 38:

You have only sent the code. What are the errors that you are having problems with? I have noticed that this code is not correct:

public int getNumberOfRestaurants() // getter method for the
NumberofRestaurants{

I would delete the comment - because it is not useful - and reformat your code like this:

public int getNumberOfRestaurants() {

Keywords: assign2


Question 36 (2024):

Submission reference: IN3681

The restaurant test code that was uploaded on moodle has two errors and won't let me compile to test my restaurant class. How do i fix this? These are the lines of code on the test class that are showing up as errors, in specific ".setFoodType" and ".setAveragePrice" :

@Test
    public void testSetFoodType()
    {
	String updatedFoodType = "Italian " + rand.nextInt(10);
	aRestaurant.setFoodType(updatedFoodType);
	assertEquals(updatedFoodType, aRestaurant.getFoodType(),
		"The correct food type was not set.");
    }

and 

@Test
    public void testSetAveragePrice()
    {
	int updatedPrice = averagePrice + 1 + rand.nextInt(20);
	aRestaurant.setAveragePrice(updatedPrice);
	assertEquals(updatedPrice, aRestaurant.getAveragePrice(),
		"The correct average price was not set.");
    }

Answer 36:

What are the errors? It is your code that is likely wrong in some way. Check that you have the correct spelling of the required method names for the Restaurant class.

Keywords: assign2


Question 35 (2024):

Submission reference: IN3680

Here is my code for the removeType method:

code deleted
I understand that you cannot remove an restaurant from the collection whilst it is iterating but I'm not sure how to fix this. In addition, is there a more concise way to write
code deleted
of the earlier methods I used it in but it seems inefficient.

Answer 35:

Remember that variable names should start with a lower-case letter, so avoid calling the variable Restaurant (upper-case 'R') because it causes confusion with the class name Restaurant.

You will need to use a different type of loop for this task. Take a look at how to iterate with a 'while loop' as the preferred solution.

You are comparing the food type correctly, so there is no need to find an alternative to that part. It is not inefficient.

Keywords: assign2 , method-removeType

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Fri Jan 17 08:25:20 2025
This document is maintained by David Barnes, to whom any comments and corrections should be addressed.