You are on page 1of 25

Lecture # 3

Classes
String
JOptionPane

Classes in Java have two very


different functions
1. A class can group together variables and
subroutines that are contained in that class.
These variables and subroutines are called static
members of the class.
example:
In a class that defines a program, the main() routine
is a static member of the class. The parts of a
class definition that define static members are
marked with the reserved word "static", just like
the main() routine of a program.

2. They are used to describe objects. the class of


an object specifies what subroutines and
variables are contained in that object. The class is
a type -- in the technical sense of a specification
of a certain type of data value -- and the object is
a value of that type.
example,
String is actually the name of a class that is
included as a standard part of the Java language.
It is also a type, and actual strings such as "Hello
World" are values of type String.

Sample program that performs a few mathematical


tasks and reports the time that it takes for the program
to run.
public class TimedComputation {
public static void main(String[] args) {
long startTime; // Starting time of program, in milliseconds.
long endTime; // Time when computations are done, in
milliseconds.
double time; // Time difference, in seconds.
startTime = System.currentTimeMillis();
double width, height, hypotenuse; // sides of a triangle
width = 42.0;
height = 17.0;
hypotenuse = Math.sqrt( width*width + height*height );
System.out.print("A triangle with sides 42 and 17 has hypotenuse
");
System.out.println(hypotenuse);
endTime = System.currentTimeMillis();
time = (endTime - startTime) / 1000.0;
System.out.print("\nRun time in seconds was: ");
System.out.println(time);
} // end main()

String
String - That object contains data, namely the
sequence of characters that make up the string. It
also contains subroutines. All of these
subroutines are in fact functions.
For example, length is a subroutine that computes
the length of a string. Suppose that str is a
variable that refers to a String. For example, str
might have been declared and assigned a value
as follows:
String str;
str = "Seize the day!";

Then str.length() is a function call that represents


the number of characters in the string. The value
of str.length() is an int
System.out.print("The number of
characters in ");
System.out.println("the string \"Hello
World\" is ");
System.out.println( "Hello World".length() );

The String class defines a lot of


functions
s1.equals(s2) is a function that returns a
boolean value. It returns true if s1 consists of
exactly the same sequence of characters as s2,
and returns false otherwise.
2. s1.equalsIgnoreCase(s2) is another booleanvalued function that checks whether s1 is the
same string as s2, but this function considers
upper and lower case letters to be equivalent.
Thus, if s1 is "cat", then s1.equals("Cat") is
false, while s1.equalsIgnoreCase("Cat") is true.
3. s1.length(), as mentioned above, is an integervalued function that gives the number of
characters in s1.
1.

s1.charAt(N), where N is an integer, returns a value


of type char. It returns the N-th character in the
string. Positions are numbered starting with 0, so
s1.charAt(0) is the actually the first character,
s1.charAt(1) is the second, and so on
5. s1.compareTo(s2) is an integer-valued function that
compares the two strings. If the strings are equal,
the value returned is zero. If s1 is less than s2, the
value returned is a number less than zero, and if s1
is greater than s2, the value returned is some
number greater than zero
6. s1.toUpperCase() is a String-valued function that
returns a new string that is equal to s1, except that
any lower case letters in s1 have been converted to
upper case. For example, "Cat".toUpperCase() is
4.

7. s1.trim() is a String-valued function that

returns a new string that is equal to s1 except


that any non-printing characters such as
spaces and tabs have been trimmed from the
beginning and from the end of the string.
Thus, if s1 has the value "fred ", then s1.trim()
is the string "fred".
8.

s1.toUpperCase(), s1.toLowerCase(), and


s1.trim(), note that the value of s1 is not
changed. Instead a new string is created and
returned as the value of the function. The
returned value could be used, for example, in
an assignment statement such as "s2 =

First GUI: JOptionPane


import javax.swing.JOptionPane
public class Welcome4
{
public static void main( String args[] )
{
JOptionPane.showMessageDialog( null, Hi Java! );

System.exit( 0 )
} // end of main()
} // end of class Welcome1

This adds an import statement, which tells


the compiler you want to use somebody elses
class.
The javax.swing is like a DOS path.

First GUI: JOptionPane


import javax.swing.JOptionPane

You must know these classes, and how to use


them.
This path helps the compiler find the class you
wish to use.
The javax.swing portion of this name is called
the package.
Classes in the same package have a connection
we will explore later.
Suffice it to say that they are very chummy.

First GUI: JOptionPane


import javax.swing.JOptionPane
public class Welcome4
{
public static void main( String args[] )
{
JOptionPane.showMessageDialog( null, Hi Java! );
System.exit( 0 )
} // end of main()
} // end of class Welcome1

The Statement JOptionPane.showMessageDialog means:


I want object JOptionPane to perform its method
showMessageDialog(). Also, Im passing the data:
null and Hi Java! In Java, we call that data
arguments.

import javax.swing.JOptionPane;
public class Welcome4
{
public static void main( String args[] )
{
JOptionPane.showMessageDialog( null,
Hi Java! );
System.exit( 0 );
} // end of main()
} // end of class Welcome4

System.exit( 0 ); This statement uses the


method exit of class System to end the application.
GUI Applications always require this statement to
terminate correctly.
Class System is imported automatically, in package
java.lang

Build An Application
Addition
When you are building an Application, there is a
set template for design that you automatically
follow.
Get in the habit of doing exactly as will be done
on the next few slides.

import javax.swing.JOptionPane;

1.) You tell the compiler to import any of the extra


classes you will be using in your Application.

import javax.swing.JOptionPane;
public class Addition
{

} // end of class Addition

2.) Define your class name, and right away place


the opening and closing brackets--with the
comment.

import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{

System.exit( 0 );
} // end of main()
} // end of class Addition

3.) Add the main method, and the System.exit( 0


) that you know it will require--include the comment.

import javax.swing.JOptionPane;

public class Addition


{
public static void main( String args[] )
{
String firstNumber,
secondNumber;

System.exit( 0 );
} // end of main()
} // end of class Addition

These two are String references. That means they


have the potential to point to objects of type String.
However,
at
this
point,
they
point
to
nothing.
4.) Include any local variables you will need in this
They
are Aempty
method.
local references.
variable is visible and accessible
only within the method.

import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;

System.exit( 0 );

} // end of main()
} // end of class Addition

Notice,
int does not
start with a
capital
letter.

5.) Now we have added three integer variables.


They are not objects. They hold three integers-without any methods or classes. number1,
number2 and number3 are called primitive

import javax.swing.JOptionPane;

String argument

public class Addition


{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
String is
int
number1,
number2,
returned
sum;

is received.

by the
method.

firstNumber = JOptionPane.showInputDialog( First Num );

secondNumber = JOptionPane.showInputDialog( Second Num );

Look at the Java Documentation for the


JOptionPane object. You will first see the
hierarchy of this object within the Java object
hierarchy:
d

This is the hierarchy for


the JOptionPane.
We will cover inheritance
starting in Chapter 8, but you
need to begin learning these API class
libraries.

The Class JOptionPane has several methods.


A classs methods are its capabilities.
For now, you should know that method
showInputDialog()
receives a String argument, and
returns a String result.

import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );

These InputDialog boxes are created by this


code.
But, since they are Strings, we

import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );

So, how do we get String numbers converted


into actual integers that we can do addition on?

We need some Object that has a method


capable of taking a String argument and
returning an integer.

import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
sum = number1 + number2;

Integer is a class. Its method parseInt()


takes a String argument and returns an int.

import javax.swing.JOptionPane;
public class Addition
The the
method
class JOptionPane
takes four
icons,showMessageDialog
you have five alternateofconstants
to choose from:
{ For
public static void main( String args[] )
arguments:
JOptionPane.PLAIN_MESSAGE
{
null -- this willString
be explained
in a later chapter
firstNumber,
JOptionPane.ERROR_MESSAGE
secondNumber;
The Sum is: + sum --this
converts the int sum into a String and
JOptionPane.INFORMATION_MESSAGE
int
number1,
concatenates it with thenumber2,
String The Sum is:
JOptionPane.WARNING_MESSAGE
sum;
Results is the message
displayed in the title bar.

JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE
defines the icon.
firstNumber = JOptionPane.showInputDialog(
First Num );
secondNumber
= JOptionPane.showInputDialog(
Second Num );
In Java, Constants
are always all upper
case, with words separated by underscores.
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber);
sum = number1 + number2;
JOptionPane.showMessageDialog( null, The Sum is: + sum,
Results, JOPtionPane.PLAIN_MESSAGE );
System.exit( 0 );
} // end of main()
} // end of class Addition

You might also like