User interaction, using objects

advertisement
Lecture 2:
Classes and Objects, using Scanner
and String
Recap
• Two categories of data types: primitives and
classes
• Examples of primitives?
– int (whole numbers)
– double (numbers with decimal points)
– boolean (true and false)
• Examples of classes?
– Math(uses static methods to calculate)
• VERY DIFFERENT FROM
– String (a sequence of characters, i.e. text)
– Scanner (used to get input from the user or a file)
"Instantiable" Classes
• With Math we use the class itself to access the
static methods like Math.random() or
Math.sqrt()
• An instantiable class (String, Scanner) is like a
template or pattern from which any number of
objects can be created
– Allows storing more complex data than primitives
– Uses object-oriented patterns to create and modify
More "Instantiable" Classes
• Objects of Instantiable Classes are more complex
than primitives
– Primitive values are just pieces of data (numbers,
characters, or true/false)
• For example, 2.3 is a value of type double
– Instances of classes, on the other hand, contain both
data and ways of acting on that data
• For example, a String object holds a series of letters (like
"the answer is ") but also comes with a way to UPPERCASE
the letters or extract parts of the String into other Strings
• Google String javadoc to get the doc
• Let's see write an example that shows Strings in
action
Demo time, Create project StringDemo
String message = new String ("to err is human, ");
String reply = new String("but not for computers!");
System.out.println( message );
System.out.println( message.toUpperCase() );
System.out.println( message + reply );
System.out.println( message + reply.replace('o','x');
double result = 53.4;
System.out.println("the answer is " + result);
Some Class related Terminology
• An instance of a class is called an object
– in the example: message, reply are objects of String
• To make a new object, use the new operator
• The actions you can invoke on an object are
called its methods
– in the example: toUpper(), replace() are String
methods
• To use a method, we use dot notation as in the
example
• To find out the details of how to use a particular
class, we use the javadoc documentation
Reading input from console
• An example of a very useful class (Scanner)
How do we get input from the user in Java?
– Answer: The Scanner class is one way.
– Scanner is described in Section 11.1.5 of text
– or check Scanner javadoc
//Since Scanner is in package java.util,
//import statement.
import java.util.*;
Example
/*
* This program takes an int from the user and prints
* out one bigger.
*/
public class ScannerExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = input.nextInt();
i = i + 1;
System.out.println(i);
}
}
Packages
• There are so many different classes that they are
organized into different packages
– To use a class from some package, you use an import
statement on the top of your source file
• Classes in this code fragment have been
underlined
– We don't need to import System and String because
they are in package java.lang, which consists of classes
so commonly used that it is always imported
automatically
Libraries
• In most programming languages, it takes a lot
of work just to do some very basic commonly
used tasks
• Solution: Just write it once, and make it
standard and available to everyone
• This bunch of pre-written code is called a
library
• The Java libraries are enormous! and still
growing...
Compile-time errors,
logic errors,
run-time errors
• Human error occurs constantly when
programming; we distinguish three main types
in this course:
– Compile-time errors (includes syntax errors)
• This means your source code isn't ``grammatically
correct''
• These errors are always caught by the compiler
• Often due to typos, or not understanding valid syntax
• Sytem.out.println("Hello world!");
• Usually easiest to correct
–Run-time error
• Caught by the JVM, but only when that
bad something actually happens (like
dividing by zero)
• Often not too hard to correct once it
happens
public class HelloWorld3 {
public static void main(String[] args) {
int i = 0;
i = i / i; //This causes a run time error (division by zero)
System.out.println(i);
}
}
– Logic error (aka semantic error)
• The ``everything else'' category
• It runs fine, but not the way you want it to
• Usually causes your program to behave in a strange,
unexpected way (see example and the fix)
• Usually due to an error in your logic when writing the
program
• Using a debugger can be helpful to figure it out
• System.out.println("Hello word!");
• Usually the hardest to correct
Lab 3
• Create Lab3 project in BlueJ
• We experiment with a home made Calculator
– using Scanner to read input numbers
– using if to do the right calculation
– using String to print result with a mesage
• Turn in procedure. When finished...
– Create Lab3.jar
– Click "Homework Uploader" link
– Enter passcode, estimated score, browse to Lab3.jar
– then click upload
Download