Introduction to Java Programming - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Introduction to Java Programming

Description:

'Frank Zappa' output now contains a literal ('Hello ') and a variable notice the space after o' ... name = 'Frank Zappa'; String title; title = 'output GUI' ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 18
Provided by: foxr
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java Programming


1
Introduction to Java Programming
  • Today we start off with basics of Java
    programming
  • In Java, all definitions are class definitions
  • What does this mean?
  • We must understand object-oriented programming
  • Early on however, we will create single,
    stand-alone classes
  • Classes comprise
  • Data members (class variables)
  • Methods
  • Methods are the processes that operate on the
    data
  • For now, our classes will not have any data
    members and the only method will be called main

2
Example Hello World
class name defined
public class HelloWorld public static void
main(String args) System.out.println("Hell
o World!")
the main method
one executable statement, it outputs a message to
System.out (a window)
used to delimit (start and stop) blocks of
code
3
Some Java Syntax
  • All classes start with a class definition
  • public class name
  • the name must match the file name, so the
    previous program MUST be stored in
    HelloWorld.java (or HelloWorld.jav, etc)
  • The main method must always appear as
  • public static void main(String args)
  • Just get used to it!
  • The single executable statement is a println, it
    outputs whatever is placed inside of the ( )
  • In this case, it outputs the literal message
    Hello World!
  • We could have it output other things as well, as
    we will see

4
Variables, Values, Literals
  • Literals are values in the program, provided by
    the programmer
  • Hello World will always appear
  • Variables however are locations where values can
    be stored and retrieved
  • The advantage of a variable is that the value can
    change, that is, the variable can hold multiple
    values
  • Values have types, in Java the types are
  • int, short, long
  • char
  • boolean
  • float, double
  • pre-defined objects (including Strings)

5
Example 2 Revised Hello World
public class HelloWorld2 public static void
main(String args) String name name
"Frank Zappa" System.out.println("Hello "
name)
declaring a variable, name is a String Set name
to store Frank Zappa
output now contains a literal (Hello ) and a
variable notice the space after o
6
Another Way to Output
  • System.out represents a window on your monitor
  • often known as the console window or the system
    window
  • Java has numerous GUI classes, one of which is
    JOptionPane, which creates pop-up windows of
    different types
  • To use this class (or others), you must import it
  • import javax.swing.JOptionPane or
  • import javax.swing.
  • Replace the System.out.println statement with an
    appropriate message to JOptionPane
  • JOptionPane.showMessageDialog(null, Hello
    World, title", JOptionPane.INFORMATION_MESSAGE)

7
Example 3 New Hello World
import GUI class
import javax.swing. public class
HelloWorld3 public static void main(String
args) String name name "Frank
Zappa" String title title "output
GUI" JOptionPane.showMessageDialog(null,
"Hello " name, title, JOptionPane.INFORMATION_
MESSAGE) System.exit(0)
add a title variable and store our title there
output message sent to pop-up window of type
INFORMATION_MESSAGE
System.exit(0) makes sure that the program
ends Once our pop-up window is closed
8
String Concatenation
  • Notice in our previous program, our output
    message was
  • Hello name
  • The is used to concatenate (join together)
    Strings
  • So this results in the word Hello followed by a
    blank followed by whatever is stored in name
  • Imagine that the int variable age stores a
    persons age, then we could output a message
    like
  • JOptionPane.showMessageDialog(null, "Hello "
    name ", you are " age " years old "
    ,title, JOptionPane.INFORMATION_MESSAGE)
  • Notice how
  • separate the literal parts of the message and the
    variables using signs
  • enclose all literals in quote marks
  • we include blank spaces inside of quote marks to
    enforce blanks

9
More on Variables
  • We divide variables among primitive types (int,
    double, char, boolean, etc) and objects (Strings
    and others)
  • We handle variables of primitive types directly
    through assignment statements
  • We handle variables of objects through message
    passing
  • Example, if x, y and z are ints, then x y z
    stores in x the value y z
  • If name is a String and we want to know how many
    characters there are, we pass name the length
    message as in
  • name.length( )
  • If we want to change a String to all upper or
    lower case characters, we do
  • name.toUpperCase( )
  • name.toLowerCase( )
  • JOptionPane.showMessageDialog() is another
    example of passing a message in this case, we
    pass the message showMessageDialog to the object
    JOptionPane

10
Getting Input
  • The easiest way to get input from the user is to
    use JOptionPane again
  • In this case, the message to JOptionPane is
    showInputDialog
  • Example name JOptionPane.showInputDialog(Ente
    r your name)
  • This opens a pop-up window with the question
    Enter your name and a box for the user to enter
    something, plus two buttons, OK and Cancel
  • When the user clicks on the Enter button or
    presses the enter key, whatever was typed in is
    returned and stored in the String name

11
Example Input and Output
import javax.swing.JOptionPane public class
HelloWorld4 public static void
main(String args) String firstName
JOptionPane.showInputDialog("Enter your first
name") String lastName JOptionPane.showInputDi
alog("Enter your last name") JOptionPane.show
MessageDialog(null, "Hello " firstName " "
lastName, "Greeting", JOptionPane.INFORMATION_M
ESSAGE) int many firstName.length( )
lastName.length( ) JOptionPane.showMessageDialog
(null, "Your name has " many " characters
in it", "Length", JOptionPane.INFORMATION_MESSAGE)
System.exit(0)
12
More on Variables Naming Them
  • Variables are always given names
  • These are called identifiers
  • We also name other things, constants, methods,
    classes
  • All identifiers must follow these rules
  • Start with a letter or _ or (we usually dont
    use though)
  • Include letters, _, , and digits
  • Not include any Java reserved words (such as
    public, class, void, etc)
  • We also like to follow certain naming conventions
  • All variables start with a lower case letter or
    _, and if the variable includes multiple words,
    each new word starts with an upper case character
    or _, as in first_name or firstName
  • Constants are always wholly capitalized
    (INFORMATION_MESSAGE)
  • Class names start with capital letters (String,
    JOptionPane)

13
More on Input
  • showInputDialog is only one way to obtain input
  • we will use it for now and study other approaches
    later
  • one restriction on using showInputDialog is that
    it can only input and return a String
  • What if you want to input a number and store it
    in an int, float, double?
  • You must use a conversion method (shown on the
    next slide)
  • The same is true if you want to store the input
    as a char which is only a single character, not
    an entire String

14
Converting Inputs
  • To convert an input into an int use
  • Integer.parseInt(JOptionPane.showInputDialog())
  • As in
  • int age Integer.parseInt(JOptionPane.showInputDi
    alog(Enter your age))
  • To convert an input into a double use
  • Double.parseDouble(JOptionPane.showInputDialog())
  • As in
  • double gpa double.parseDouble(JOptionPane.showIn
    putDialog(Enter your GPA))
  • To convert an input into a char use
  • JOptionPane.showInputDialog().charAt(0)
  • As in
  • char sex JOptionPane.showInputDialog(Enter
    your sex).charAt(0)

15
More on Strings
  • On the previous slide, we saw that we could take
    a String and turn it into a character by using
    charAt
  • In fact, charAt takes 1 character from the String
    and returns it
  • We use charAt by passing it as a message to the
    String
  • Example firstName.charAt(0) returns the first
    letter, perhaps to be used to determine a
    persons initial
  • The value passed to charAt must be an int between
    0 and the length of the String 1
  • To determine the length, use .length( ) as in
    name.length( )
  • Other String messages
  • replace(oldchar, newchar)
  • substring(startingpoint, endingpoint)
    endingpoint is actually the character after the
    ending point in the substring
  • toUpperCase( ), toLowerCase( ), length( )
  • concat(str) takes the String str and
    concatenates it to this String, so that
    str1.concat(str2) does the same as str1 str2

16
String Using Example
import javax.swing. public class
StringUsingExample public static void
main(String args) // get
the user's full name in 3 inputs String
firstName JOptionPane.showInputDialog("Enter
your first name") String middleName
JOptionPane.showInputDialog("Enter your middle
name") String lastName JOptionPane.showInputD
ialog("Enter your last name") String name
firstName " " middleName " "
lastName name name.replace('e', '3') //
store in name the new String once we name
name.replace('E', '3') // replace each of 'e',
'i', 'g', also name name.replace('i',
'1') // replacing the upper case
versions name name.replace('I', '1') name
name.replace('g', '6') name
name.replace('G', '6') // output a
response JOptionPane.showMessageDialog(null,
"Hello " name ".\nI do not like the
letters 'e', 'i' or 'g'\n" "so I have
replaced them with '3', '1' and '6'",
"Response", JOptionPane.INFORMATION_MESSAGE)
System.exit(0) // end the program once the
dialog window is closed
17
Comments
  • To wrap up our discussion, notice our last
    program used comments
  • Comments appear after // symbols or between /
    and /
  • Comments do nothing at all in the program
  • But they are useful to explain chunks of code to
    the programmers who write, modify, debug or
    otherwise view your code
  • So it is a good habit to get into to include
    comments where you feel they are necessary to
    explain what you are trying to do
Write a Comment
User Comments (0)
About PowerShow.com