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

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Review. By the end of today you should be able to- Know how to use args Know how to use the JOptionPane Know how to convert a String to a number.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Computer Programming Lab(4).
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 2 Elementary Programming
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
Chapter 2: Java Fundamentals
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Today’s topic: Arithmetic expressions.
Computing Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
Chapter 2: Basic Elements of Java
Java Variables, Types, and Math Getting Started
Chapter 2: Java Fundamentals
Chapter 3 – Introduction to C# Programming
elementary programming
Unit 3: Variables in Java
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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

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

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 typeVariable nameValue Single quotation 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 B My 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; 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

Examples of String String name = “Chonho”; String job = “Student at UMB”; String id = “ ”; String = System.out.println( name + “ information” ); System.out.println( “Who? “ + job + “, ID: “ + id ); System.out.println( “Please at “ + ); On the screen, you will see Chonho information Who? Student at UMB, ID: Please at

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