SlideServe
Menu
Préférences
© SlideServe · v2026

Introduction to Computer Programming

nay avatar
13 diapositives · 288 Views · Oct 06, 2014

Learn how to create a tip calculator in Java using JOptionPane dialog boxes, classes, object instantiation, and Scanner class for user input. Practice parsing strings for numeric values.

Introduction to Computer Programming - slide 1
01 / 13Introduction to Computer Programming
1 / 17

Télécharger la présentation

Introduction to Computer Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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 !!