Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Ch 2: Data and Expressions (2 of 2)
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
}
}
d) 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 + "'");
}
}
Graphics
// BasicGraphics.java
// A simple shell for drawing graphics as described in our textbook.
// Note that this is an *application* whereas the book uses *applets*.
// This should have little impact on you, however.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class BasicGraphics extends JComponent {
public static void main(String[] args) {
JComponent myGraphics = new BasicGraphics(); // Your class name goes here!
launch(myGraphics, 500, 300); // Set the initial dimensions here!
}
public void paint(Graphics page) {
int width = getWidth();
int height = getHeight();
// paint a blue rectangle as large as the window
page.setColor(Color.blue);
page.fillRect(0,0,width,height);
// paint a yellow oval inscribed in the blue rectangle
page.setColor(Color.yellow);
page.fillOval(0,0,width,height);
}
//////////////////////////////////////////////////////////////////////////////
///////////////// END OF YOUR CODE /////////////////////
///////////////// (you may ignore all the code below!!! /////////////////////
//////////////////////////////////////////////////////////////////////////////
public static void launch(JComponent jc, int width, int height) {
JFrame frame = new JFrame(jc.getClass().getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(jc);
frame.setContentPane(cp);
frame.setSize(new Dimension(width,height));
frame.setVisible(true);
}
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem