HW00 - james-bern/CS136 GitHub Wiki
![](https://private-user-images.githubusercontent.com/5091875/362848995-89773ba8-46aa-4a94-92a0-2a7d58bde708.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUxODY0MjcsIm5iZiI6MTcyNTE4NjEyNywicGF0aCI6Ii81MDkxODc1LzM2Mjg0ODk5NS04OTc3M2JhOC00NmFhLTRhOTQtOTJhMC0yYTdkNThiZGU3MDgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDFUMTAyMjA3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjU3M2ZjOGMzYmNmOGJjMzdiNmY5ZDdiMjliYzI5MmU0OGViNDhkYWE5OTNmNDFiYWRmOWQ4ZDgyY2YyNzc2ZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.3antaa_wjiRNp-Lp-ZE3fMJFtpaSAQ0rSYvTSvAREaE)
In this quick mini homework, we will learn the basics of type and scope by translating Python code into Java.
Here are some Java primitive types (not-Object's):
- An
int
stores a (signed) integer like-1
,0
, or42
- A
char
stores a character like'A'
,'b'
,'7'
, or'?'
- A
double
stores a real number like0.0
,3.1415
, or-0.001
- A
boolean
storesfalse
ortrue
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
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
{
int i;
{
int j;
// you CAN use i here 🙆
// you CAN use j here 🙆
}
// you CAN use i here 🙆
// you CANNOT use j here 🙅
}
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
- 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
- In your HW00.java file, implement a Cow.java version of this Python 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
class HW00 extends Cow {
public static void main(String[] arguments) {
int i = 42 / 0
ASSERT(2 + 2 == 5);
PRINT(i);
}
}
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
}
}