Computer Science 15-100 (Sections T & U), Fall 2007
Class Notes, Day 4: Thu 6-Sep-2007
Logistics
Topic Outline:
public class MyCode {
public static void main(String[] args) {
// Demonstrate the different integer primitive data types
long xlMax = Long.MAX_VALUE;
int xiMax = Integer.MAX_VALUE;
short xsMax = Short.MAX_VALUE;
char xcMax = Character.MAX_VALUE;
byte xbMax = Byte.MAX_VALUE;
long xlMin = Long.MIN_VALUE;
int xiMin = Integer.MIN_VALUE;
short xsMin = Short.MIN_VALUE;
char xcMin = Character.MIN_VALUE;
byte xbMin = Byte.MIN_VALUE;
// Note: you do not yet need to know about "printf" (soon...)
System.out.printf("%5s %25s %25s\n","type","max","min");
System.out.printf("%5s %25d %25d\n","long", xlMax,xlMin);
System.out.printf("%5s %25d %25d\n","int", xiMax,xiMin);
System.out.printf("%5s %25d %25d\n","short",xsMax,xsMin);
System.out.printf("%5s %25d %25d\n","char", (int)xcMax, (int)xcMin);
System.out.printf("%5s %25d %25d\n","byte", xbMax,xbMin);
}
}
public class MyCode {
public static void main(String[] args) {
// Demonstrate the different integer primitive data types
float xfMax = Float.MAX_VALUE;
double xdMax = Double.MAX_VALUE;
float xfMin = Float.MIN_VALUE; // Not what you may expect!!!
double xdMin = Double.MIN_VALUE;
// Note: you do not yet need to know about "printf" (soon...)
System.out.printf("%5s %25s %25s\n","type","max","min");
System.out.printf("%5s %25g %25g\n","float", xfMax,xfMin);
System.out.printf("%5s %25g %25g\n","double", xdMax,xdMin);
}
}class MyCode {
public static void main(String[] args) {
char c1 = 'A';
char c2 = (char)66;
char c3 = "ABCD".charAt(2);
System.out.println("c1 = '" + c1 + "'");
System.out.println("c2 = '" + c2 + "'");
System.out.println("c3 = '" + c3 + "'");
}
}class MyCode {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = (b1 && b2); // b1 AND b2
boolean b4 = (b1 || b2); // b1 OR b2
boolean b5 = !b1; // NOT b1
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
System.out.println("b3 = " + b3);
System.out.println("b4 = " + b4);
System.out.println("b5 = " + b5);
}
}class MyCode {
public static void main(String[] args) {
int i = 10;
double d = 10;
System.out.println( 10 / 3 );
System.out.println( i / 3 );
System.out.println( d / 3 );
System.out.println( 10 / 3.0 );
System.out.println( i / 3.0 );
}
}class MyCode {
public static void main(String[] args) {
double d1 = (29 / 7.0) * 7.0;
double d2 = 29;
System.out.println(d1 == d2);
System.out.println(d2 - d1);
}
}See Figure 2.4 (p. 78)
a) As Statements
class MyCode {
public static void main(String[] args) {
int x = 5;
System.out.println(x); // 5
x++;
System.out.println(x); // 6
++x;
System.out.println(x); // 7
x--;
System.out.println(x); // 6
--x;
System.out.println(x); // 5
}
}
b) As Expressions
class MyCode {
public static void main(String[] args) {
int x = 5, y = 0;
System.out.println(x + "," + y); // 5,0
y = x++;
System.out.println(x + "," + y); // 6,5
y = ++x;
System.out.println(x + "," + y); // 7,7
y = 10 + x--;
System.out.println(x + "," + y); // 6,17
y = 10 + --x;
System.out.println(x + "," + y); // 5,15
}
}class MyCode {
public static void main(String[] args) {
int x = 5;
System.out.println(x); // 5
x += 2;
System.out.println(x); // 7
x *= 2;
System.out.println(x); // 14
x %= 9;
System.out.println(x); // 5
x /= 2;
System.out.println(x); // 2
x -= 5;
System.out.println(x); // -3
}
}a) Widening (automatic) versus Narrowing (manual, lossy)
b) Assignment Conversion
class MyCode {
public static void main(String[] args) {
int a = 5;
double b = a; // Assignment conversion!
System.out.println("a = " + a); // 5
System.out.println("b = " + b); // 5.0
}
}
c) Promotion
class MyCode {
public static void main(String[] args) {
int a = 5;
double b = (2.0 + a); // Promotion!
System.out.println("a = " + a); // 5
System.out.println("b = " + b); // 7.0
}
}
c) Casting
class MyCode {
public static void main(String[] args) {
double a = 5.0;
int b = a; // Will not compile!
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
So we cast the double into an int:
class MyCode {
public static void main(String[] args) {
double a = 5.0;
int b = (int) a; // Casting!
System.out.println("a = " + a); // 5.0
System.out.println("b = " + b); // 5
}
}
Note that casting truncates:
class MyCode {
public static void main(String[] args) {
int a = (int)5.9;
int b = (int)-5.9;
System.out.println("a = " + a); // 5
System.out.println("b = " + b); // -5
}
}a) From p. 87: Just need to know the
methods next***()
next(), nextLine(), nextBoolean(), nextByte(), etc...
b) next() versus nextLine()
Does not work quite as you might expect:
* next returns next input token as a String
* nextLine returns the rest of the current line as a String
* they do not mix-and-match well!
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2;
System.out.print("Enter a few words: ");
s1 = scanner.next();
System.out.print("Enter a few other words: ");
s2 = scanner.nextLine();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
}
}
Do this again, but just use next, not nextLine:
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2, s3;
System.out.print("Enter a few words: ");
s1 = scanner.next();
s2 = scanner.next();
s3 = scanner.next();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
System.out.println("s3 = '" + s3 + "'");
}
}
Once more, but just use nextLine, not next:
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2;
System.out.print("Enter a few words: ");
s1 = scanner.nextLine();
System.out.print("Enter a few other words: ");
s2 = scanner.nextLine();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
}
}
import javax.swing.*;
import java.awt.*;
public class Test extends JComponent {
public static void main(String[] args) {
runApplication(new Test()); // change "Test" to the name of your class!
}
public void paint (Graphics page) {
final int MID = 150;
final int TOP = 50;
// setBackground(Color.cyan);
page.setColor(Color.cyan);
page.fillRect(0,0,getWidth(),getHeight());
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5); // left eye
page.fillOval (MID+5, TOP+10, 5, 5); // right eye
page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect (MID-15, TOP-20, 30, 25); // top of hat
}
public static void runApplication(JComponent component) {
component.setOpaque(true);
JFrame frame = new JFrame(component.getClass().getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(component);
frame.getContentPane().setBackground(Color.white);
frame.setSize(300,300);
frame.setVisible(true);
}
}
Carpe diem!