Unit 2 -- Csc 115 Spring 2005 SO1/S02
Learning Objectives
Continued review of Csc 110 material:
- To understand how casting works in general and to be able to cast
between primitive and reference types when required
- To know how to pass parameters by value and how references can be used to
simulate passing parameters by reference.
- To understand the concepts behind and to be able to use and choose between the
various mechanisms to do flow control in Java
- To understand how to construct expressions, and how to make use
of the mathematical and string operators
- To be able to construct and use arrays
- To understand the basics of String objects, how to create and concatenate
strings
- To be able to do simple I/O
Learning Resources
Lecture slides: 2-IntroToJava.ppt
Reading: Chapter 1 in the textbook
Sample programs
Self-Study Activities
Answer the following questions:
-
Experiment with the following code
(if using Gild, explore the Dr.Java Interactions pane)
and make sure you understand the output:
int [] a;
a = new int[5];
a[0] = 42;
a[0]
a[1]
a[5]
a.length
String[] answers = {“yes”, “no”};
answers[0]
answers.length
int [] b = {12, -15, 42, 12, 10};
b[5] = 11;
- What is the difference between a while statement and a
do while statement?
-
How can you translate a for loop into a while loop?
Show how with an example and check it works in the Interactions Pane.
- Why is a String not an array of characters?
- Explain how we can simulate "pass by value" in Java.
Try playing with the following code in your code editor
and make sure you understand the output and explain the results:
int i = 3;
double d = 3.2;
double dresult;
dresult = i/d // if displaying in the interaction pane, skip the semicolon to view the output
Now try this example... what happens and why doesn't it work?
int i = 3;
double d = 3.2;
int iresult;
iresult = i/d; // there is a problem here, why?
Problems in the textbook, Chapter 1:
See the following problems in the book:
- R-1.1 (check your answers using your code editor)
- R-1.2 (copy, paste and then edit the relevant
code example from the book, and check your answer!)
- R-1.3 (copy, paste and then edit the
code example from the book, and check your answer!)
- R-1.10 (check your answer by typing and running it!)
- R-1.14 (check your answer by typing it in and running it!)
- C-1.2 -- there are at least two ways to solve this one --
try finding a solution that uses the & operator
(check your answer in your programming environment!)
(note: C-1.2 is different in edition 3 as follows:
Write a Java method that takes an array of int values and determines if
all the numbers are distinct from each other (that is, they are distinct).
Go to the course website