Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.

Similar presentations


Presentation on theme: "Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers."— Presentation transcript:

1 Using Data Within a Program Chapter 2

2  Classes  Methods  Statements  Modifiers  Identifiers

3  Classes  The class is the essential Java construct.  A class is a template or blueprint for objects.  Methods  Main method is a collection of statements that performs a sequence of operations  Statements  Represents an action or a sequence of actions  Every statement in Java ends with a semicolon (;) Class { main { Statements; }

4 4  Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used.  Common modifiers:  public and static public static void main {  Other modifiers:  private, final, abstract, and protected.

5 Three types of comments in Java 1. Line comment: A line comment is preceded by two slashes (//) in a line 2. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines 3. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command.

6  Used to represent a piece of data  Actually represents memory location where data is stored  Identifier used to name variable Should begin with lower case letter Subsequent words use upper case Should be meaningful  Example: salesTaxRate

7  Constant  Cannot be changed while program is running  Variable  Named memory location  Use to store value  Can hold only one value at a time  Value can change

8  Data type  Type of data that can be stored  How much memory item occupies  What types of operations can be performed on data

9  Type int  Store integers, or whole numbers  Value from –2,147,483,648 to +2,147,483,647  Variations of the integer type  byte  short  long

10  Floating-point number  Contains decimal positions  Floating-point data types  float  double  Significant digits  Refers to mathematical accuracy

11  Boolean logic  Based on true-or-false comparisons  Boolean variable  Can hold only one of two values  true or false boolean isItPayday = false;  Relational operator  Compares two items

12  char data type  Holds any single character  Place constant character values within single quotation marks char myMiddleInitial = 'M';  String  Built-in class  Store and manipulate character strings  String constants written between double quotation marks String myName = “Tim”;

13  Arithmetic with variables or constants of same type  Result of arithmetic retains same type  Arithmetic operations with operands of unlike types  Java chooses unifying type for result  Unifying type  Type to which all operands in expression are converted for compatibility

14  Every variable in a Java program must be declared before it is used  A variable declaration tells the compiler what kind of data (type) will be stored in the variable  The type of the variable is followed by one or more variable names separated by commas, and terminated with a semicolon  Variables are typically declared just before they are used or at the start of a block  Basic types in Java are called primitive types int numberofdays; double totalWeight;

15  Variable given no particular value when declared  Assignment gives it a value  Syntax variable = expression ; Expression evaluated Value stored in memory location specified by variable

16 16  Declare multiple variables in separate statements on different lines int myAge = 25; int yourAge = 19;

17  To find the area of a circle, we use the formula:  Area = (Pi) * (radius) 2 radius A = π r 2

18 public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } no value radius no value area allocate memory for radius and area

19 public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 20 radius 1256.636 area compute area and assign it to variable area assign value to radius Print message to console

20  System.out is an object that is part of the Java language  println is a method invoked by the System.out object that can be used for console output  The data to be output is given as an argument in parentheses  A plus sign is used to connect more than one item  Every invocation of println ends a line of output System.out.println("The answer is " + 42);

21  Another method that can be invoked by the System.out object is print  The print method is like println, except that it does not end a line  With println, the next output goes on a new line  With print, the next output goes on the same line

22  Libraries in Java are called packages  A package is a collection of classes that is stored in a manner that makes it easily accessible to any program  In order to use a class that belongs to a package, the class must be brought into a program using an import statement  Use dialog box to display: import javax.swing.JOptionPane; JOptionPane.showMessageDialog()

23 import javax.swing.*; public class YourNameDialog{ public static void main(String[ ] args){ String result; result = JOptionPane.showInputDialog (null, "What is your name? "); JOptionPane.showMessageDialog (null, "Hello " + result); } }

24  Input data through the keyboard by using the Scanner class in the utility library import java.util.Scanner;  Create a Scanner object Scanner input = new Scanner(System.in);  To obtain the value use the methods:  next()for string data type  nextByte()for byte  nextShort()for short integer  nextInt()for integer  nextLong()for long integer  nextFloat()for floating point  nextDouble()for double floating point  nextBoolean() for boolean value

25 import java.util.Scanner; public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius System.out.print("Enter your radius: "); Scanner input = new Scanner(System.in); radius = input.nextDouble(); // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }

26 import javax.swing.*; public class ComputeArea { public static void main(String[ ] args) { double radius; double area; String input; // Assign a radius input = JOptionPane.showInputDialog (null, "Enter the radius "); radius = Double.parseDouble(input); // Compute area area = radius * radius * 3.14159; // Display results JOptionPane.showMessageDialog (null, "The area for the circle of radius " + radius + " is " + area); } Dialog Box only uses String data Converts the string to a double format

27  A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter a whole number or decimal number: ");  Always echo all input that a program receives from the keyboard  In this way a user can check that he or she has entered the input correctly


Download ppt "Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers."

Similar presentations


Ads by Google