Introduction to Computer Programming

1 / 13

# Introduction to Computer Programming - PowerPoint PPT Presentation

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 :

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## Introduction to Computer Programming

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi

2. Agenda • Project Questions • FTP • Dialog Boxes • Classes and Objects • Scanner Class

3. 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. 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. 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. 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. 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. 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. 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); } } Attributes • 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(); • } Method Creating an instance of a class

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

12. 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. THANKS !!