﻿﻿ CS 1110/1111: Introduction to Programming

Lecture 8

Announcements

1st year SEAS: via the normal Dean's office declaration form
2+ year SEAS: as CS Office in Rice hall, now through February 17
CLAS BA: at CS Office in Rice hall, February 24 through March 17
CLAS transfer to SEAS: via the normal Dean's office declaration form
SEAS Minor: http://www.cs.virginia.edu/~horton/cs-minor-apply by March 1
CLAS Minor: not offered

HW1 - what it will tell you when

Talking Points

Pick up where we left off

Strings and excape sequences: `"I said \"hi\" to them"` the `\"` is a quote character instead of the end of a string. `"The word is \""+word+"\""`

APIs on the Collab Wiki

Scanners: all you need is one (per input source)

Integer division

Casting – in the same way that `-x` means give me a negative version of x, `(int)x` means give me an integer version of x. In the same way that `-"hi"` doesn't make sense, `(int)"hi"` also doesn't make sense.

We almost always put parentheses around the thing being cast

```public class CastingDivision {
public static void main(String[] args) {
System.out.println( (int)(3.14) );
System.out.println( (double)(3) );
System.out.println( (char)(68) );
char c = 'w';
c = (char)(c - 3);
System.out.println(c);
System.out.println( 9 / 5 );
System.out.println( 9.0 / 5 );
System.out.println( 9 / 5.0 );
System.out.println( 9.0 / 5.0 );
System.out.println( (double)(9 / 5) );
System.out.println( (double)(9) / 5 );
}
}```

Constants: `public static final`

```   public class Confusing {
public static void main(String[] args) {
Scanner cheese = new Scanner(System.in);
System.out.print("Enter number: ");

int n = cheese.nextInt();
double s = 0.00471 * n;
int g = (int)(s);

System.out.println("ans = " + g); }}```
```public class MP3Sizer {

// accepted value for gigabytes per song
public static final double GIGABYTES_PER_SONG = 0.00471;

// main(): program starting point
public static void main( String[] args ) {

// set up input scanner
Scanner stdin = new Scanner( System.in );

// get number of songs
System.out.print( "Enter number of songs: " );
int nbrSongs = stdin.nextInt();

// determine expected number of gigabytes to store songs
double gigaBytes = GIGABYTES_PER_SONG * nbrSongs;

// drop the decimal
int storage = (int) ( gigaBytes );

// display result
System.out.println( "Amount of storage necessary for " + nbrSongs
+  " songs is " + storage + " GB" );
}
}```

DecimalFormat

e notation: `13e-4` means 13 × 10−4; read e as times ten to the

The Math library: `Math.pow(4.0651,5)`, `Math.sqrt(2)`, `Math.PI`, etc

```DecimalFormat twoPlaces = new DecimalFormat("0.00");
DecimalFormat oneOrTwoPlaces = new DecimalFormat("0.0#");
double n1 = 1.0 / 99;
double n2 = Math.pow(1111, 0.59999);
String s1a = twoPlaces.format(n1);
String s1b = oneOrTwoPlaces.format(n1);
String s2a = twoPlaces.format(n2);
String s2b = oneOrTwoPlaces.format(n2);
System.out.println(n1 + " " + s1a + " " + s1b);
System.out.println(n2 + " " + s2a + " " + s2b);
```

Finite precision: ±2 billion for `int`, about 16 decimal places for `double`

```System.out.println(1000 * 1000 * 1000);
System.out.println(1000 * 1000 * 1000 * 1000);
System.out.println(1.23456789 + 1 - 1);
System.out.println(1.23456789 + 1e10 - 1e10);
System.out.println(1.23456789 + 1e20 - 1e20);
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
```