Date post: | 20-Dec-2015 |
Category: |
Documents |
View: | 221 times |
Download: | 1 times |
Lecture 9
Today’s topic
• Let’s continue to do exercise (Lecture 8)
• Character data type– Char
• String class– String
Exercise 1
• Mile to Kilometer conversion
1 Mile is 1.6 Kilometer
Create a calculator application to convert unit from Mile to Kilometer
- Ask an user to enter one number in mile
- Calculate the unit conversion
- Print out the result in Kilometer for the user.
public class MyCalculator{ static final double MileToKilometer = 1.6;
public static void main(String[] argv) {
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setSize(500, 300);
frame.setVisible(true);
console.println( "This program converts mile to kilometer.” ); double mile = console.readDouble( “Enter number in mile: “ );
double kilo = MileToKilometer * mile; console.println( mile + “ mile is " + kilo + “ kilometer");
}}
Exercise 2 (comment out previous one)
• Fahrenheit Tf to Celsius Tc conversion
Conversion formulaTc = (5 / 9) * (Tf – 32)
Create a calculator application to convert unit from degree Fahrenheit to Celsius
- Ask an user to enter number in Tf- Calculate the unit conversion- Print out the result in Tc for the user.
public class MyCalculator{
public static void main(String[] argv) {
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setSize(500, 300);
frame.setVisible(true);
console.println( "This program converts F degree to C degree.” ); double Tf = console.readDouble( “Enter degree in F: “ );
double Tc = (5.0/9.0) * (Tf – 32); console.println( Tf + “ F degree is " + Tc + “ C degree");
}}
Extra methods in IOConsole
setForeground( Color )where Color will be one of colors such as
Color.Red, Color.Blue, Color.Green, etc. (page 43)
setFont( Font )where Font will be Font object as follow(page 42)
Font font = new Font( family, style, size );family: “Serif”, “SansSerif”, “Monospaced”,
etcstyle: Font.PLAIN, Font.BOLD, Font.ITALICsize: integer
Extra methods in JFrame
setLocation( int x, int y )
move a window to (x, y) position of the screen
public class MyCalculator{
public static void main(String[] argv) {
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
Font font = new Font( “Serif”, Font.ITALIC, 20);
console.setFont( font );
console.setColor( Color.GREEN );
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setLocation(200,300);
frame.setSize(500, 300);
frame.setVisible(true);
Challenge
• If you want to do calculation twice or more without closing windows, then you can use loop to ask users for entering new numbers.
console.println( "This program adds two numbers.” );
for( int i=0; i<5; i++){ console.println( "This program converts F degree to C degree.” ); double Tf = console.readDouble( “Enter degree in F: “ );
double Tc = (5.0/9.0) * (Tf – 32); console.println( Tf + “ F degree is " + Tc + “ C degree");
}
Exercise 3 (comment out previous one)
• Circle circumference and areaPI = 3.14circumference = 2 * PI * radiusarea = PI * (radius)2
radius
• Let’s create a new class, named MyCircle– So, the file name should be MyCircle.java– MyCircle is an application to calculate the
circumference and area of the circle, whose radius is provided by users
Exercise 3 (start with this)
/* File name: MyCircle.java by Chonho Lee -------------------------- This program calculates the circumference and area of
circle */
import acm.io.*;import java.awt.*;import javax.swing.*;
public class MyCircle{
}
Comment
Import packages
Today’s topic
• Character data type– Char
• String class– String
Character
• Character is one of primitive data types of variable (such as integer and double)
• Character variable contains one character as its value– Alphabet: a, b, c,…,A, B, C,…– Digit: 0, 1, 2, …– Symbol: +, -, =, !, <, >, $, %, …
Declaration of Character Variable
We use keyword char
Similar to integer and double
char ch = ‘b’;
Data type Variable name Value
Singlequotation mark
Examples of char variable
char grade = ‘B’;char plus = ‘+’;char num5 = ‘5’;
System.out.println( “My grade is “ + grade );System.out.println( “My grade is “ + grade + plus);System.out.println( num5 + plus + num5 + “ is 10“ ); On the screen, you will see
My grade is BMy grade is B+5+5 is 10
Strings
• String is basically just a collection of characters.
• Thus, the string “O’Bryant” could be thought of as a sequence of 8-elements ('O', '’', ‘B', 'r', ‘y', ‘a‘, ‘n’, ‘t’).
Strings in java
• String is a class in Java• Strings are constant (values cannot be changed
once they are created)
• Set of characters “Chonho Lee" is a string.
• "A" is a string, 'A' is a character.Note:Characters are delimited by single quotes and Strings are delimited by double quotes.
Capital !
int val = 10; val 10
String name = new String(“Chonho”);
String name = “Chonho”;
Simplify!
Since String is a classCreate object! (declare object)
Declare variable
Declaration
String classname Chonho
Examples of String
String name = “Chonho”;String job = “Student at UMB”;String id = “4816828”;String email = “[email protected]”
System.out.println( name + “ information” ); System.out.println( “Who? “ + job + “, ID: “ + id );System.out.println( “Please email at “ + email );On the screen, you will see
Chonho informationWho? Student at UMB, ID: 4816828Please email at [email protected]
Today’s Exercise
• Name card creator– Let’s create a name card by name card
creator application– Write a class, named MyNameCard
• Ask an user to enter first name• Ask an user to enter last name• Ask an user to enter age• Print out the results
public class MyNameCard{
public static void main(String[] argv) {
JFrame frame = new JFrame(“My Calculator");
IOConsole console = new IOConsole();
frame.getContentPane().add(BorderLayout.CENTER, console);
frame.setSize(500, 300);
frame.setVisible(true);
console.println( "This program create a name card for users” ); String first = console.readLine( “Enter first name: “ ); String last = console.readLine( “Enter last name: “ ); String age = console.readLine( “Enter your age: “ ); console.println( “Hi, my name is “ + first + “ “ + last ); console.println( “I am “ + age + “ years old” );}
}