HW00 - james-bern/CS136 GitHub Wiki

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
- 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
- In Java, a function must say what type it returns.
-
double foo(...) { ... }
returns adouble
-
void bar(...) { ... }
does not return anything ("returnsvoid
")
-
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-
- Complete Tut00
- A
- In your HW00.java, convert the Python program below into Java (technically, Cow.Java)
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.0710...
B = (A < 10.0)
if B:
C = 0
for i in range(5):
C += i
print(C) # 10 (NOT 10.0)
D += C
assert (D > 20.0)
print(D) # 22.071...
- A+
- Share a fun fact about yourself in the last question of the HW00 submission page on Glow.
- Please delete the two lines with variable
i
in them from the starter code once you finish Tut00 - You must translate the program line by line; do NOT try to simplify the program
- You must include the original comments
- Your code must print
10
, NOT10.0
- Use Cow.Java’s
ASSERT(...)
function instead of Java'sassert ...
statement. (Java'sassert
is disabled by default, and it's too easy to forget to enable it)
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) {
...
}
(TODO (Jim): Make this part of the reading) 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
}
}
👀
class HW00A extends Cow {
static double D;
static double pythag(double a, double b) {
double result = SQRT(a * a + b * b);
D += result;
return result;
}
public static void main(String[] arguments) {
double A = pythag(3.0, 4.0);
PRINT(A); // 5.0
A = pythag(A, A);
PRINT(A); // 7.0710678118654755
boolean B = (A < 10.0);
if (B) {
int C = 0;
for (int i = 0; i < 5; ++i) {
C += i;
}
PRINT(C); // 10
D += C;
ASSERT(D > 20.0);
PRINT(D); // 22.071067811865476
}
}
}