CS 112
Spring 2023

Old version

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

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. In the programs for Part II, should we reprint the values entered by the user after they enter them?

    No.

  3. 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.

  4. For Problem 5, I can’t figure out what condition I should use to determine if the dosage is a whole number. 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.

    In crafting the necessary condition, it may help to realize that Java is happy to let you write a condition that compares an integer to a double. For example, the condition

    5 == 5.0
    

    is valid, and it produces the boolean value true.

    If you can’t figure out how to construct the necessary condition 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 dosage with no decimal when it it is a whole number?

    Here again, a type cast may be helpful!