Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9. Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String.

Similar presentations


Presentation on theme: "Lecture 9. Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String."— Presentation transcript:

1 Lecture 9

2 Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String

3 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.

4 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"); } }

5 Exercise 2 (comment out previous one) Fahrenheit Tf to Celsius Tc conversion Conversion formula Tc = (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.

6 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"); } }

7 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”, etc style: Font.PLAIN, Font.BOLD, Font.ITALIC size: integer

8 Extra methods in JFrame setLocation( int x, int y ) move a window to (x, y) position of the screen

9 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);

10 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"); }

11 Exercise 3 (comment out previous one) Circle circumference and area PI = 3.14 circumference = 2 * PI * radius area = 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

12 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

13 Today’s topic Character data type –Char String class –String

14 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: +, -, =, !,, $, %, …

15 Declaration of Character Variable We use keyword char Similar to integer and double char ch = ‘b’; Data typeVariable nameValue Single quotation mark

16 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 B My grade is B+ 5+5 is 10

17 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’).

18 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 !

19 int val = 10; val10 String name = new String(“Chonho”); String name = “Chonho”; Simplify! Since String is a class Create object! (declare object) Declare variable Declaration String class name Chonho

20 Examples of String String name = “Chonho”; String job = “Student at UMB”; String id = “4816828”; String email = “chonho@gmail.com” 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 information Who? Student at UMB, ID: 4816828 Please email at chonho@gmail.com

21 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

22 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” ); }


Download ppt "Lecture 9. Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String."

Similar presentations


Ads by Google