CS101 - Lab 1

Introduction to Java programming: basic instructions and conditionals

The goal of this lab is to get you confortable using the basic instructions in Java: communicating with the user (reading and writing), declaring and manipulating variables, and using if and if-else statements. Start by reviewing the problems we did in class and read carefully the lecture notes. Then go ahead and start on the problems.

You will most likely encounter many problems in this first lab. Before you raise your hand for help, try to understand what happens with your program. If it is a compile problem, read the messages on the screen; the compiler tries to give you a hint of what went wrong; can you figure out what it's telling you?

If your program compiles, but when run it does not give you quite the desired output, then we say that it has bugs. Try to debug it. Try to understand what happens, what are the instructions that are being executed, in what order. To debug, people use lots of "print" instructions. Print out the values of the variables that are relevant; this will help you understand how values change and when the errors occurs.

Programming style: Pay attention to issues of programming style:

If you develop a good programming style, programming will come easier to you (plus, I wont take points off). And it will be easier for others to help you.

Work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.

  1. Alladin's treasure cave

    Imagine you are Alladin, and you just found the treasure cave. You know you cannot come back to it, so you want to take away with you as much as possible. You can see the following in the cave:

    But, irony, you only have your backpack!! Everything you take must fit into your backpack.

    The question is, what items to take away with you in the backpack (i.e. steal), so that they (fill the backpack and) are worth as much as possible. Imagine there are plenty of stones around, so you don't need to worry that you can take more than there is available. For example, if your backpack can hold 100 grams, then...you would take 4 diamonds. If your backpack can hold 120 grams, you would take 4 diamonds and two sapphires. If your backpack can hold 117 grams, you would take 4 diamonds, 1 sapphires, one ruby and 2 pearls.

    Write a program that asks the user for the capacity of the backpack, in grams, and prints out the optimal way to fill in the backpack with treasures from the cave. Your program should print how many of each stone to take, and the total value of the backpack in the end.

    The interface of your program should look something like this (feel free to improve and personalize it!!):

    Hi Alladin, I am your good genie. 
    What can I do for you today? 
    Oh, I remember now. I'll help you fill your backpack. 
    Tell me how big is your backpack (in grams): 117
    There you go. Get  
    4 diamonds
    1 sapphires
    1 ruby
    2 pearls
    total: 117 grams, 223 thousand dollars
    Now let me go to sleep..

    Before you start programming, think how you would solve this problem.
    You'll see that you can solve this problem using only the basic instructions that you have learnt! (Isn't that amazing?) I am giving you only one hint: use division and remainder. Their meaning is:

    34/5 is 6
    the remainder of 34/5 is  4

    Start the Java programming only after you know how to solve the problem. In Java, the operator used to get the remainder is %. Try the following:

    int a, b, res, rem; 
    a = 34;
    b = 5; 
    res = a/b; //res stores the result of a/b
    rem = a%b; //rem stores the remainder of a/b
    System.out.println("result is " + res);
    System.out.println("remainder is " + rem);

    Use the following skeleton for your program, where I separated out the logical blocks (You can download this file from Alladin.java). Fill in the missing parts.

    This is the skeleton of the program that helps Alladin get rich. 
    Laura Toma
    csci 107
    public class Alladin {
    	public static void main (String args[]) {
    		//variables to store values; units are in thousands
    		int DIAMOND_VALUE = 50; 
    		int SAPPHIRE_VALUE = 15; 
    		int RUBY_VALUE = 6; 
    		int PEARL_VALUE = 1; 
    		//variables to store WEIGHTS; units are in grams
    		int DIAMOND_WEIGHT = 25;
    		int SAPPHIRE_WEIGHT = 10;
    		int RUBY_WEIGHT = 5;
    		int PEARL_WEIGHT = 1;
    		//variable to read from user
    		ReadStream r = new ReadStream(); 
    		//declare more variables
    		//print welcome message
    		//read the weight of the backpack from user
    		//find out optimal numbers of each stone
    		//print out results to Alladin
    	} //end of main
    } // end of class

What to turn in:

Send me only the .java file (not the entire folder), as attachment.