CSC 150 Chapter 8: High Level Languages: Java
primary information resource:
An Invitation to Computer Science (Java), Third Edition,
G. Michael Schneider and Judith L. Gersting,
Course Technology, 2007.
[ previous | schedule | next ]
High Level Languages (HLLs)
Motivate need for HLLs – disadvantages of assembler language:
HLLs designed to address these shortcomings
HLL is more abstract from machine structure
Java combines these two:
General remarks on Java program structure
application/applet/JavaScript
main
packages
program files
Data in Java programs
Java variable identifiers implement same concept as labeled .DATA in our assembly language: names that refer to memory locations containing data.
AKA variables, data storage whose contains can be changed.
There are other kinds of identifiers in Java:
public class Worker {
public float grossWages(int hours, float rate) {
final int fulltime = 40;
float gross;
gross = hours * rate;
if (hours > fulltime) {
gross = gross + (hours-fulltime)
*
rate * 0.5;
}
done: return gross;
}
}
In machine/assembly language, data were defined in a cluster at the end of instructions, to keep them out of the instruction flow. In Java and most other HLLs, this is not necessary or even allowed. Variable and constant identifiers (e.g. labels for data) must be declared before being used.
Textbook says that Java variable declarations are usually collected together at the top of the code (the C language requires this, but C++ and Java do not). Other guidelines recommend declaring a variable “just in time”, just before its first usage.
Variable declaration has form: type variable;
where type is the name of its data type, and variable
is the identifier you’ve chosen.
You can specify an initial value using the form: type variable =
value;
where type and variable are as above, and value
is a valid constant value of the same type.
There are a number of primitive types:
All others are object types (classes):
String is the most frequently used object type. It is not part of the Java language, but is a class provided in the java.lang package.
You may also declare and use arrays. An array is:
Instructions in Java programs
Java statements fall into one of three categories: sequential, conditional, iterative
input and output instructions
There are various classes of I/O devices:
The Java
API includes packages for GUI interfaces (e.g. AWT or Swing) which
can be used to interact via keyboard/mouse/monitor using WIMP techniques.
Java 1.5 update: Java 1.5 (aka 5.0) includes a new class in the java.util
package called Scanner that you can use for easy input from the keyboard (or just about
any other input source). Example:
Scanner scan = new Scanner(System.in); // select keyboard as input source
int i = scan.nextInt(); // transfer integer input into variable
Programming text-based console output in Java is not so bad, but still
fairly primitive.
- Use statement System.out.print(stringExpression);
to output a character string.
- Use statement System.out.println(stringExpression);
to output a character string followed by newline character.
- In both cases, stringExpression is either a String
or something that can be converted into a String (any primitive
type, or object type that has a “toString” method).
The Java
API includes a variety of classes for file I/O (e.g. FileInputStream,
FileOutputStream)
and network I/O (e.g. Socket).
Graphics programming and the textbook/Lab 14 examples
We covered the basics of computer graphics hardware in CSC 100.
See my notes on this subject at
http://faculty.otterbein.edu/PSanderson/csc100/notes/lecture07.html
Graphics are drawn within a rectangular window called a frame
Example: given frame which is 100 pixels high and 200 pixels
wide,
Graphic2.java exemplifies the structure of the Lab 14 graphics
programs.
Here it is, annotated with line numbers for each reference.
1 // Graphic2.java
2 import java.awt.*;
3 public class Graphic2 extends LabFrame{
4 int left = 100;
5 int top = 100;
6 public void paint(Graphics g){
7 setBackground(Color.black);
8 g.setColor(Color.white);
9 g.drawString("Click
anywhere to draw a rectangle", 10, 40);
10 g.drawRect(left, top,
30, 30);
11 }
12 public void mousePressed(int x, int y){
13 left = x;
14 top = y;
15 repaint();
16 }
17 public static void main(String[] args){
18 LabFrame f = new Graphic2();
19 f.setSize(300, 300);
20 f.setVisible(true);
21 }
22 }
This is the program that draws a small rectangle with its upper-left corner based on the position of the mouse each time you click.
Comments on this code:
Line(s) | Comment |
3 | This application is an extension of LabFrame (see LabFrame.java), which itself extends Frame, one of Java's AWT classes. |
6, 12, 17 | These are the typical methods found in the Lab 14 programs. The Graphic2 class inherits quite a few methods. The mousePressed method is "automatically" called when a mouse button is pressed over the window. The paint method is called anytime the window is changed and needs to be drawn again. |
17-21 | main kicks things off by creating and showing a frame (window). |
12-16 | mousePressed records the mouse position at the time of the button press (lines 13 and 14), and forces the frame to be repainted with a rectangle at the new mouse position. |
15 | repaint is inherited. It is a method which in turn calls the paint method. |
6-11 | When a frame is changed, only its data structure is affected. This occurs in lines 13 and 14. In order to make the change visible, the Graphics object associated with the frame needs to be completely painted (drawn) again. This method specifies how to paint it. The Graphics object is parameter g. |
10 | This draws the rectangle itself. There are four parameters: x-coordinate of upper left corner, y-coordinate of upper left corner, width in pixels, and height in pixels. Note that rectangle size is constant but position depends on mouse position at the time of the click (at the time mousePressed was called). |