Class 1 notes

Welcome to CS115/IT115 Intro to Java Part 2

Betty (really Elizabeth) O'Neil
Department of Computer Science
University of Massachusetts Boston

Goals A student who successfully completes CS115 should:

  • Understand and be able to decompose a problems into parts and program them
  • Understand how to use Java objects
  • Be prepared for the next CS course, CS210  Advanced Algorithms and Data Structures

Web Page: www.cs.umb.edu/it115

Go there and then bookmark the resulting page. The source for all things for the class--

  • Schedule
  • Homework assignments
  • Lecture notes
  • Contact information

Textbook --go over syllabus 

Note first thing to do: get a departmental account for it115: see instructions on syllabus.

Two types of assignments: 1. There are 5-7 programming assignments (p1, p2, ...) 2. Written assignments (HWs) (hw1, hw2, ...)

Exams: midterm and final

Final Grade Your final grade will be determined from the sum of your assignment and exam grades (simple point system)

  • Class participation 30 pts. (5%) including attendance
  • Homeworks 80 pts. (13.3%)
  • Programming projects 240 pts. (40%)
  • Midterm 100 pts. (16.6.%)
  • Final 150 pts (25%)

Homework, programming assignments: You can discuss concepts, but not share code or written details

Please bring your text to class, so we can look at examples easily.

Working from home You can set up the same environment at home as in the PC labs here, or better We provide no support, officially, but you can bring your laptop to office hours and I'll try to help. Demo next week.

Software is free, and not difficult to install: see online docs.  We will be using DrJava, as used in cs110 and cs210.

The Math Resource Center offers tutoring 30 Minute Sessions, By appointment only, starting in 3 weeks (hopefully) More info then. Call 617-287-6550

Where are we in Java?

In CS114, you have covered the fundamentals of the C part of Java, that is, Java without user-defined objects. Remember this state of things for CS240, where you learn actual C. In C, there are no objects, although there are "structs" which have some properties of objects. 

Living without objects has restricted the examples to number-driven things, plus a few Strings, which are actually objects, as are arrays. With objects, we can reasonably model a Person, with name, address, age, etc., all stuck together in one object. We can build up bigger objects out of smaller ones, so that a Group object can have an array of Person inside it, for example. Mastering the use of Java objects will be the main topic of this course, but we need to cover static methods first (Chap 2) to finish the C-like Java coverage. 

Looking more closely at where we are, it appears that you have covered Chap. 1-7. Thus you know how to use loops and if statements, and arrays, the workhorse of data crunching programs, and files.

You know how to get a sequence of numbers into a program, then how to loop through them, apply formulas to them, and output the results.

To help with formulas, you know how to use Java's Math class, so you can call Math.sqrt, etc., listed on pg. 1119. One of these is not a "formula" in the usual sense: Math.random() returns a random number (double) between 0 and 1. Important Random methods are on pg.1120.

 

Important String methods are on pg. 1121, along with Scanner methods. Character methods are on pg. 1115. The rest of these methods will be useful in the future.

Output from a program is easy.
For example:   System.out.println("Result is " + x);

Input to a program
You have studied two ways to get values into a program, from program arguments and from "standard input', the user's keyboard or via "input redirection" from a text file.

Input to a program from program arguments: args[0], args[1], etc., provided to the program when it starts via String array "args". So args[0] is a String, say "5" Apparently not used in CS114--OK


Input to a program from standard input

Once the program is running, it may request additional data from "standard input", which is just the user's keyboard in the simple case. The Scanner class helps us with interpreting this text, and I see it’s been in use in the book since Chap 3, pg. 165—go over.

Primitive Types

Basic list, pg. 62, last four, pg. 1128, of which long is the most important.

 

Note that String is not here. A String is an object.  It has methods, like length() and startsWith(“A”);  Arrays are also objects.


If it's a really big number, it may not fit in an int.

Since an int can hold 32 bits, it can hold 2^32 binary patterns, half of which are used for positive numbers and the other half for negative numbers. Half of 2^32 is 2^31. There are 2^31 numbers that are 0 or positive, and 2^31 numbers that are negative.

How big is 2^31?

Powers of two: what you should know!
2^10   (two to the tenth power)  = 1024, known as "1K", K for "kilo", is approximately 1000 = 10^3
So 2^20 = (1024)^2, is approximately 10^6 = one million, and is known as "1M", M for mega
2^30 = (1024)^3, is approximately 10^9 = one billion, and is known as "1G", G for giga

2^31 is twice 2^30, so there are a little over two billion positive numbers that can fit in an int.

So the simple rule is that numbers "under 2 billion" positive or negative fit in an int, so all 9-digit numbers fit, but not all 10-digit ones do.

If you need numbers bigger than that, just switch to longs (or doubles, but longs are known as being whole numbers.)

If it's not a whole number, we need to use double.  No problem, Scanner can handle it for us.

If it's not a number at all, and being input from a user, treat it as a String.

The Structure and Terminology of a Method Declaration

Example:  method of name “total”, return type int, parameter “int x”:

public static int total(int x)
{

  int y = x + 10;

  return y;

}

 

The method signature (defined on pg. 147) is the name and parameters, such as total(int x) or just total(int).

The method header is the name, parameters and return value, such as int total(int x)

The method body is everything inside the outermost curly braces, and the braces.

The method declaration is the method header and method body.

 

When we use this method, it looks like this:

int w = total(10);  

We say we are calling total with argument 10.  An argument of 10 becomes a parameter of value 10 inside the method.

The idea of an API.

The “Java API” is all the methods of all the classes in the JDK. See pg. 1123 for part of its online documentation. See the link on the syllabus and try it out asap.

Example: scroll down to find Scanner, click on it so see all the Scanner methods.

But the idea of API is not just for Java classes.  We can use it for our own code.

In general, an API is a set of method headers that together provide a summary of useful code.

For example, the NumberGuess3 program on pg. 354-355: we can say it has an API as follows:

// print out instructions

public static void giveIntro(); 

// get and return a guessed number from the user, between 0 and 99

public static int getGuess(Scanner console); 

// check a guess against the secret number, return number of matching digits

public static int matches(int number, int guess);

 

Code that calls these methods can be called the client of the API.  Here the client code is in main. 

See how it calls giveIntro() to get a printout of game rules, then generates the secret number using Random, the calls getGuess(console) to get a guessed number from the user.

Then it checks the guessed number against the secret number by calling matches(…), and reports info to the user, and loops if needed for more guesses.


The API is a useful summary of what the code outside of main can do for us.  In fact, we can move the code of main into another class—we’ll do that some time.

Since API methods should be public to be useful, we could drop the “public”’s here.

If two programmers are working to create a program, the API can be the “contract” of how the two pieces of code fit together.

One programmer: writes user interface code, calling API as needed.

Other programmer: writes the code that implements the API.

The Java interface (Chap 9) will help us express APIs, ones that are implemented by objects anyway. We need to cover how to set up our own objects first—that’s in Chap 8.