Upload
nay
View
17
Download
0
Embed Size (px)
DESCRIPTION
Introduction to Computer Programming. Recitation 3 Ihsan Ayyub Qazi. Agenda. Project Questions FTP Dialog Boxes Classes and Objects Scanner Class. Project: Any Questions?. Goal : Make a Tip Calculator Input : Total bill Tip percentage Number of people splitting the bill Output : - PowerPoint PPT Presentation
Citation preview
Ihsan Ayyub Qazi ([email protected])
Introduction to Computer Programming
Recitation 3Ihsan Ayyub Qazi
Ihsan Ayyub Qazi ([email protected])
Agenda
Project Questions FTP Dialog Boxes Classes and Objects Scanner Class
Ihsan Ayyub Qazi ([email protected])
Project: Any Questions? Goal: Make a Tip Calculator Input:
Total bill Tip percentage Number of people splitting the bill
Output: Tip amount Total to pay Total per person
Requirements: JOptionPane class must be used for input and output Atleast two different data types must be used
Extra Credit: Use DecimalFormat class
Ihsan Ayyub Qazi ([email protected])
Dialog Boxes: JOptionPane Class
Java API has a class that allows you to create dialog boxes JOptionPane
import javax.swing.JOptionPane; JOptionPane.showMessageDialog(null,
"Hello"); JOptionPane.showInputDialog("Enter name");
returns a String How can we convert a String into a number?
Why not casting? Strings are not primitive data types?
Ihsan Ayyub Qazi ([email protected])
Converting Strings to numbers
byte age = Byte.parseByte(“7"); short year = Short.parseShort(“1007"); int bigNum = Integer.parseInt(“82456"); long bigNum2 =
Long.parseLong("6743210"); float price = Float.parseFloat("1.99"); double sum =
Double.parseDouble("72.345");
Ihsan Ayyub Qazi ([email protected])
Output?import javax.swing.JOptionPane; public class Test{ public static void main(String args []) {
String input; float value;int con;
input = JOptionPane.showInputDialog("Enter your GPA);value = Float.parseFloat(input);con = value; JOptionPane.showMessageDialog(null, “Your GPA is ”+con);
}}
Ihsan Ayyub Qazi ([email protected])
What is a class? An object? Consider a bicycle of a particular model from a
given company
Thousands of such bicycles may have been manufactured and sold
Each bicycle was built from the same set of blueprints and therefore contains the same components
In object-oriented terms, we say that your bicycle is an instance of the class (of objects) known as bicycles.
A class is the blueprint from which individual objects are created.
Ihsan Ayyub Qazi ([email protected])
Variables
What does a primitive type variable store? holds data items
What does class type variables store? holds references (or address of an object)
Ihsan Ayyub Qazi ([email protected])
Bicycle class: Output?public class Bicycle {
int speed = 0; int gear = 1;
void changeGear(int newValue) { gear = newValue;
}
void speedUp(int increment) { speed = speed + increment;
}
void applyBrakes(int decrement) { speed = speed - decrement; }
void printStates() { System.out.println(" speed:"+speed+" gear:"+gear);}}
public static void main(String args []) {
Bicycle bike = new Bicycle();
bike.speed = 34; bike.gear = 3; bike.changeGear(5); bike.speedUp(3); bike.printStates(); bike.applyBrakes(5); bike.printStates();
}
Attributes
Method
Creating an
instance of a class
Ihsan Ayyub Qazi ([email protected])
In-class problem
Create two objects of type Bicycle name them bike1 and bike2
The output of the program should be speed:10 gear:2 // bike 1 speed:7 gear:2 // bike 2 speed:30 gear:4 // bike 1 speed:32 gear:4 // bike 2 speed:5 gear:2 // bike 1 [bicycle goes down, tough
luck !]
speed:40 gear:5 // bike 2
Ihsan Ayyub Qazi ([email protected])
Quick Review: Scanner Class
Another method for getting user input! One can get input in different data formats import java.util.Scanner; Some Scanner Class methods
Method Name Returns Input As
nextByte byte
nextDouble double
nextFloat float
nextInt int
nextLine String
nextLong long
nextShort short
Ihsan Ayyub Qazi ([email protected])
public class Payroll{ public static void main(String[] args) { String name; // To hold a name int hours; // Hours worked double payRate; // Hourly pay rate double grossPay; // Gross pay // Create a Scanner object to read input. Scanner keyboard = new Scanner(System.in); // Get the user's name. System.out.print("What is your name? "); name = keyboard.nextLine(); // Get the number of hours worked this week. System.out.print("How many hours did you work this week? "); hours = keyboard.nextInt(); // Get the user's hourly pay rate. System.out.print("What is your hourly pay rate? "); payRate = keyboard.nextDouble(); // Calculate the gross pay. grossPay = hours * payRate; // Display the resulting information. System.out.println("Hello " + name); System.out.println("Your gross pay is $" + grossPay); }}
Ihsan Ayyub Qazi ([email protected])
THANKS !!