Download presentation
Presentation is loading. Please wait.
Published byLogan Taylor Modified over 9 years ago
1
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi
2
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) Agenda Project Questions FTP Dialog Boxes Classes and Objects Scanner Class
3
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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
4
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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?
5
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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");
6
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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); }
7
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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.
8
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) Variables What does a primitive type variable store? holds data items What does class type variables store? holds references (or address of an object)
9
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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(); } Attribute s Method Creating an instance of a class
10
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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
11
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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 NameReturns Input As nextBytebyte nextDoubledouble nextFloatfloat nextIntint nextLineString nextLonglong nextShortshort
12
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) 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); }
13
Ihsan Ayyub Qazi (ihsan@cs.pitt.edu) THANKS !!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.