Using Predefined Classes - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Using Predefined Classes

Description:

public static String valueOf(double d); - returns double d as a String ... Define double and assign it to a value. Determine if the last digit in the number is a 3. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 26
Provided by: clemen4
Category:

less

Transcript and Presenter's Notes

Title: Using Predefined Classes


1
Strings
and
Pre-Defined Classes
2
Using Predefined Classes
  • Static variables and methods
  • Fundamentals of Strings
  • Browsing class packages

3
Static Modifiers
  • The keyword static associates a variable or
    method with a
  • class, rather than with an object of a class.
  • A static variable/method is not referenced
    through a particular
  • instance of a class, but through the class
    itself.
  • A static variable is also known as a class
    variable. Other variables are known as instance
    variables.
  • A static method is also known as a class
    method. Other
  • methods are known as instance methods.
  • A class method cannot reference instance
    variables, which only
  • exist in an instance of a class.

4
Static Modifiers
public class MyMath public static final
double PI 3.14 private int maxint
public static double square(double n)
return( n n ) // end
square public void setmaxint(int i)
maxint i // end setnextint // end
MyMath
5
import javax.swing. public class AreaTest
public static void main(String args)
String input double area,radius
input JOptionPane.showInputDialog("What is
radius") radius Double.parseDouble(input)
area MyMath.PI MyMath.square(radius)
JOptionPane.showMessageDialog(null,"The
area is " area " square in.","Area",
JOptionPane.PLAIN_MESSAGE)
System.exit(0) // end main // end
AreaTest
  • Variable PI and method square are referenced
    through the Class.

Run Rrogram
6
(No Transcript)
7
Java Strings
  • A string is a series of characters treated as
  • a single unit
  • Examples hello Florida
  • 245-90
  • A string in Java is an object of class String.
  • String FirstName
  • String School, j

8
Java Strings
  • There are two ways to create a String object.

- Instantiation (use one of the
constructors) String LastName new
String(Allen)
- Dynamically String FirstName Barney
  • The characters in the String are number 0
    length - 1.

9
STRING METHODS
public char charAt( int index) - returns the
character located at position index. - Example
FAMU The character at position 0 is
F
the character at position 3 is U
public String concat( String str) - returns a
string that is str added to the object string -
Example Jack be concatentated with
nimble gives Jack be
nimble
10
public class Vertical public static void
main(String a ) int j String
name new String(JANE) char ch
for(j0 j lt name.length( ) j)
ch name.charAt( j )
System.out.println( ch ) // end for
// end main // end Vertical
Run Program
11
  • Create a String that has some text in it.
  • Display the first and last characters in the
    String.

12
STRING METHODS
public boolean endsWith(String suf) - returns
true if the object string has suf as a suffix -
Example er is a suffix of rattler
ttl is not a suffix of
rattler
public boolean startsWith(String pre) - returns
true if the object string has pre as a prefix -
Example ratt is a prefix of rattler
le is not a prefix of
rattler
13
import javax.swing. public class CheckWord
public static void main(String a )
String N1, N2 String input
N1 JOptionPane.showInputDialog("Enter a
word ") if( N1.endsWith("er") )
JOptionPane.showMessageDialog(null,"This
word ends with er","CheckWord",
JOptionPane.PLAIN_MESSAGE)
else JOptionPane.showMes
sageDialog(null,"This word does not end with
er","CheckWord", JOptionPane.PLAIN_MESSA
GE) System.exit(0) // end
main // end CheckWord
Run Program
14
(No Transcript)
15
  • The String class has a set of static methods
    (overloaded) for converting other data types to
    strings

public static String valueOf(int i) - returns
integer i as a String - Example 3467
valueOf gives 3467

public static String valueOf(char c) - returns
c as a String - Example c valueOf gives
c
16
  • More valueOf methods...

public static String valueOf(double d) -
returns double d as a String - Example
3467.98 valueOf gives 3467.98

public static String valueOf(boolean b) -
returns boolean value b as a String - Example
true valueOf gives true

17
  • Define double and assign it to a value.
  • Determine if the last digit in the number is a 3.

18
  • Define an integer and assign a value to it.
  • Convert the integer to a String.

19
Browsing the JAVA API - to look for classes - to
look for methods - to just look
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com