Title: Basics Simple Arithmetic
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Basics Simple Arithmetic

Language Fundamentals:

Java is case sensitive: string , String are 2 different things

Java is white space insensitive.

Spaces, new lines, and tabs are ignored by the compiler unless inside of quotes:

However, I will ask you to wisely use spaces, tabs and newlines to make your code more readable to humans even though the compiler could care less.

Comments:

All programming languages provide some mechanism for creating comments. Comments provide insite to program execution for programmers. Comments are ignored by the compiler. For example, here is our hello program again with comments. From a compilation and execution point of view it is identical to the previous program. However, this version contains comments that are available to the reader of the program.

// All programs in Java live inside of some class:
class Hello {
       // Any program that is started from the Java program will need a method with the name of main.


       public static void main(String[] args /* This parameter contains program command line parameters*/)
       {
             System.out.println("hello world"); // The remainder of this line is ignored by the compiler
       }
       /*  Multiple line comment
           Second line in the comment
           Third line in the comment
      */
     }


Variables and Primitive Data Types

Consider the following program:


import java.util.Scanner;

public class PrimativeVariables {


    public static void main(String[] args) {
        int num1;   // variable to hold our first input
        int num2;   // variable to hold our second input
        int answer; // variable to hold our answer

        // Open up the keyboard for user input
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter first number:");
        num1 = keyboard.nextInt();

        System.out.println("Enter second number:");
        num2 = keyboard.nextInt();

        answer = num1 + num2;  // simple variable arithmetic

        System.out.println("The answer ="+ answer);
    }

}

Things to note and try:

- Note that the comments are ignored by the compiler.
- Rework the above to declare all variables on the same line
- Note that "=" really means replace ... its not exactly the way you would think in Algebra
- Note that we can name our variables anything we want. As your program gets larger, it becomes more important to pick good names for variables that are used over a wide area of your program.
- Normal algebraic expressions can be written using normal algebra symbols like "-", "/", "*", "(", ")" --- rework the above program by modifying the expression below:
num1 + num2
- Instead of int, you can try short, byte, double, float (note there are methods nextShort, nextByte, etc.). See the table below for a description of the various primitive data types:

The following code was created in the video for this set of notes:


import java.util.Scanner;


public class PrimativeVariables {


    public static void main(String[] args) {
        int num1;
        int num2;
        int answer;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter first number");

        num1 = keyboard.nextInt();
        System.out.println("Enter second number");
        num2 = keyboard.nextInt();
        // + - * /   %

        answer = num1  * num2;
        System.out.println("The answer for multiply is " + answer);

        answer = num1 / num2;
        System.out.println("The answer for divide is " + answer);

        answer = num1 % num2;
        System.out.println("The answer for % is "+ answer);

    }

}


Java Practice(JPHA)

1. (2 points) Suppose someone has 10, 5, and 1 dollar bills in his wallet. Create a program that asks the user how many $10, $5, $1 dollar bills he has. Your program would then print out how much his money is worth. For example three 10's, two 5's and two 1's would be worth $42.
2. (3 points) Suppose a person wants wants to carry around the fewest number of bills in his wallet. Create a program that figures out how many $10, $5, and $1 dollar bills he needs to have the smallest number of bills that are worth a certain amount of money. Try your program on $47 worth of money.

Hint: Let's suppose that you have a variable named "worth" which is an integer and contains the
value of the money in your wallet.  You would want to maximize the number of the largest bill ($10).

int number_of_10s = worth/10;

The amount remaining to be covered with $5 and $1 bills
would be the "remainder" from dividing by 10 or:

int remaining = worth % 10;
Now the number of $5 is:
int number_of_5s = remaining / 5;

Now you can continue this logic to figure out new value for "remaining".
Hint: what does (remaining % 5) return.
You continue the logic from here.


Last Modified: December 23, 2014 3:23 PM