HW00 - james-bern/CS136 GitHub Wiki

README

⚠️ NOTE: You must complete Tut00 before attempting this homework.

In this quick mini homework, we will learn the basics of type and scope by translating Python code into Java.

Java

Type

Here are some Java primitive types (not-Object's):

  • An int stores a (signed) integer like -1, 0, or 42
  • A char stores a character like 'A', 'b', '7', or '?'
  • A double stores a real number like 0.0, 3.1415, or -0.001
  • A boolean stores false or true

Java is a statically-typed language.

  • When we declare a variable, we have to say what type it is.
    int a = 5; // "make an int called a with value 5"
    int b; // "make an int called b but leave its value undefined for now"
  • A variable's value CAN change 🙆
    int b = 5; // "make an int called b with value 5"
    b = 6; // "set the value of b to 6"
  • A variable's type CANNOT change 🙅
    int b = 5; // "make an int called b with value 5"
    b = false; // 🚨 COMPILE ERROR: Type mismatch: cannot convert from boolean to int
  • A variable must be declared before it is used.
    b = 5;  // 🚨 COMPILE ERRROR: i cannot be resolved to a variable

Scope

A scope is a region of code in which variables live

  • In Java, a scope is (usually) defined by a pair of curly braces
  • OUTER_SCOPE { INNER_SCOPE } OUTER_SCOPE

Example

{
    int i;
    {
        int j;
        // you CAN use i here 🙆
        // you CAN use j here 🙆
    }
    // you CAN use i here 🙆
    // you CANNOT use j here 🙅
}

More Examples

int i = 0;
i = 1;
i = 1; // 🚨 COMPILER ERROR: i cannot be resolved to a variable
int i = 0;
int i = 0;
{
    i = 1;
    PRINT(i); // 1
}
PRINT(i); // 1
int i = 0;
{
    int i = 1; // 🚨 COMPILER ERROR: Duplicate local variable i
}
{
    int i = 0;
}
i = 1; // 🚨 COMPILE ERRROR: i cannot be resolved to a variable

TODO's

  • A
    • In your HW00.java file, implement a Cow.java version of this Python program
      • NOTE: You must translate the program line by line; do not try to simplify the program
def pythag(a, b):
    global D

    result = (a * a + b * b) ** 0.5
    D += result
    return result

A = pythag(3.0, 4.0)
print(A) # 5.0

A = pythag(A, A)
print(A) # 7.0710678118654755

B = (A < 10.0)
if B:
    C = 0
    for i in range(5):
        C += i
    print(C) # 10
    D += C

print(D) # 22.071067811865476

Starter Code

			class HW00 extends Cow {
public static void main(String[] arguments) {		
				int i = 42 / 0
ASSERT(2 + 2 == 5);
				PRINT(i);
	}
				}

👀 Hints

How do I take a square root in Cow.java?

See our Documentation for the SQRT(...) function

How do I write a basic for loop in Java?
for (int i = 0; i < 10; ++i) {
    ...
}
How do I structure a simple Cow.java script with global variables, functions, and a main?

Example:

// "wrapper class"
class HW00 extends Cow {
	
	// global variables
	static int i = 0;
	
	// functions
	static double doStuff(double a) {
		++i;
		return a * a;
	}
	
	// main
	public static void main(String[] arguments) {
		double b = 3.0;
		
		b = doStuff(b);
		PRINT(b); // 9.0
		
		b = doStuff(b);
		PRINT(b); // 81.0
		
		PRINT(i); // 2
	}
	
}
⚠️ **GitHub.com Fallback** ⚠️