CS 112
Spring 2022

Old version

This is the CS 112 site as it appeared on May 12, 2022.

Problem Set 1 FAQ

Don’t forget that there are additional questions and answers on Piazza.

Using VS Code

  1. I’m still unclear about the procedure for working in VS Code. Can you remind me of the key steps?

    1. First, make sure that you have created a folder for the assignment or lab that you are working on.

    2. Next, if there is are starter files for the problem, make sure that you download them into the correct folder. If your browser doesn’t allow you to specify where a file should be saved, try right-clicking on the download link and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.

    3. In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created. (Note: You must open the folder; it is not sufficient to simply open the file.)

      The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the names of any starter files that are already in the folder.

    4. To edit an existing file, click on the name of the file in the Explorer pane, which will open an editor window for that file.

    5. To create a new file, select the File->New File menu option, which will open up an empty editor window. Then select File->Save, and give the new file the correct name, based on the name of the class. For example, if the name of the class is Foo, you would give it the name Foo.java.

    6. When you are ready to test your code, run the program by using the F5 key, or by right-clicking the name of the program in the Explorer pane and choosing Run or Run Java.

  2. When I try to run a program, I get a message that says “Cannot find a class with the main method.” Why is this happening?

    Make sure that your program includes a main method with the required header. If there are any syntax errors in the header of the main method, the compiler will give you this message.

  3. When I try to run a program, I get a message that says “Build failed, do you want to continue?” Why does this happen?

    There are two types of cases that can produce this message:

    1. If you see problems listed for the program that you’re trying to run, you need to fix them before proceeding. Click Cancel, fix the problems, and then try to rerun the program.

      Note that clicking on the description of a given problem will typically highlight the place in your code that is causing the problem.

    2. If there are no problems listed for the program you are trying to run, the message probably stems from the fact that there are problems in another file that it is in the same folder. In that case, you should be able to click the Proceed button to run your program.

Part II

  1. When I run one of programs for Part II, the program prints the correct prompt and waits for me to enter a value, but I don’t seem to be able to enter the value. What am I doing wrong?

    You may need to click on the Terminal area at the bottom of VS Code in order to enter a value that the program is waiting for you to input.

  2. For one of the problems from Part II, the results that I’m getting are not correct. Do you have any suggestions?

    Make sure that you are making your computations as precise as possible. In particular, don’t forget that there are two types of division – integer division and floating-point division. Make sure that you use the correct type of division for a given problem.

    See the next question for a related issue.

  3. For one of the Part II problems, some of my results seem to be off by a very small amount. For example, on problem 6, when I enter a height of 20 and an angle of 30, the result should be 40.0, but I get 40.00000000000001. Do I need to worry about this type of thing?

    No. If the difference between the expected and computed values is very small, you don’t need to worry about it. The floating-point representation used for real numbers is naturally imprecise, and there’s no easy way around that imprecision.

  4. For Problem 5, I can’t figure out what condition I should use to determine if the cost is a whole number of dollars. Do you have any suggestions?

    As the problem mentions, there is more than one way to do this, and one way involves using a type cast. Think about how you could use a type cast as part of a condition that would allow you to distinguish between whole-number and non-whole-number values.

    If you can’t figure out how to do this using a type cast, there is an alternative way to construct the condition that involves using one of the arithmetic/numeric operators that we discussed in lecture.

  5. For Problem 5, how can I display the cost with no decimal when it it is a whole number of dollars?

    Here again, a type cast may be helpful!

  6. When I submit my code for Problem 5, I’m getting a message from the Autograder that looks like this:

    This test produced a result that differs in at least one way 
    from the expected result.
    
    expected:
    The cost of the trip is $24.
    
    actual:
    200
    The cost of the trip is $24.
    

    I don’t understand what is wrong here.

    The problem is that you are echoing/reprinting the user’s inputs. You aren’t supposed to do that.

    In the sample runs that we provided, the lines that show the three inputs are from the portion of the program that uses Scanner methods to read the inputs from the user.

    After getting those inputs from the user, you just need to take those inputs, perform the required computations, and print the cost of the trip. You should not reprint the inputs themselves.