1 / 22

Unit Conversion Calculator: Mile to Kilometer and Fahrenheit to Celsius

Create a calculator application that converts units from Mile to Kilometer and Fahrenheit to Celsius. Users can enter a number in miles or degrees Fahrenheit and the program will calculate and display the converted value in kilometers or degrees Celsius.

aedgar
Download Presentation

Unit Conversion Calculator: Mile to Kilometer and Fahrenheit to Celsius

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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’; Singlequotation mark Data type Variable name Value

  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 Capital ! • 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.

  19. Declaration Declare variable int val = 10; val 10 Since String is a classCreate object! (declare object) String name = new String(“Chonho”); String class name Simplify! Chonho String 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” ); } }

More Related