Introduction to Java (All the Basic Stuff) Learning Objectives - - PowerPoint PPT Presentation

introduction to java all the basic stuff learning
SMART_READER_LITE
LIVE PREVIEW

Introduction to Java (All the Basic Stuff) Learning Objectives - - PowerPoint PPT Presentation

Introduction to Java (All the Basic Stuff) Learning Objectives Java's edit-compile-run loop Basics of object-oriented programming Classes objects, instantiation, methods Primitive types Math expressions


slide-1
SLIDE 1

Introduction to Java
 (All the Basic Stuff)

slide-2
SLIDE 2

CS 6452: Prototyping Interactive Systems

Learning Objectives

  • Java's edit-compile-run loop
  • Basics of object-oriented programming
  • Classes

− objects, instantiation, methods

  • Primitive types
  • Math expressions
  • Output, input
  • Strings

2

slide-3
SLIDE 3

CS 6452: Prototyping Interactive Systems

Programs

  • Python – interpreted

− Interweaves translation and execution

  • C – compiled

− High-level source code − Assembly code − Machine language code

  • Java is a kind of hybrid

3

slide-4
SLIDE 4

CS 6452: Prototyping Interactive Systems

Overview

4

Source
 code

Java
 bytecode

Java compiler Java interpreter Example.class java javac Example.java

slide-5
SLIDE 5

CS 6452: Prototyping Interactive Systems

Java Downloads

  • JRE – Java runtime environment
  • JDK – Java SE Development Kit
  • Differences

5

slide-6
SLIDE 6

CS 6452: Prototyping Interactive Systems

Getting Java

6

slide-7
SLIDE 7

CS 6452: Prototyping Interactive Systems

Test It

7

slide-8
SLIDE 8

CS 6452: Prototyping Interactive Systems

Gentle IDE

8

slide-9
SLIDE 9

CS 6452: Prototyping Interactive Systems

JGrasp UI

9

compile run

slide-10
SLIDE 10

CS 6452: Prototyping Interactive Systems

Classes & Objects

  • Object-oriented programming
  • Classes

− Model or blueprint from which objects are made

  • Object

− State (attributes) − Behaviors (methods)

10

Car analogy

slide-11
SLIDE 11

CS 6452: Prototyping Interactive Systems

Program

  • Made up of classes

− Each class in its own file (mostly)

  • Class named Tiger goes in the file

Tiger.java

11

slide-12
SLIDE 12

CS 6452: Prototyping Interactive Systems

Small Example

12

File Test.java

// An example program public class Test { public static void main(String[] args) { System.out.println("Hi there"); } } /* A longer comment that goes across multiple lines */

slide-13
SLIDE 13

CS 6452: Prototyping Interactive Systems

What is that?

13

public class Test { public static void main(String[] args) { System.out.println("Hi there"); } }

Class named Test Modifiers
 (explain later) Return type
 (nothing) Method (function) named main Parameter type Parameter var name

slide-14
SLIDE 14

CS 6452: Prototyping Interactive Systems

Compile & Run

14

Compile Run

slide-15
SLIDE 15

CS 6452: Prototyping Interactive Systems

Java Programs

  • Strongly typed

− Variables must be declared − Cannot change type later − Can only mix compatible types

  • All whitespace the same
  • Statements separated by ;
  • Case sensitive

15

slide-16
SLIDE 16

CS 6452: Prototyping Interactive Systems

Entities

  • Two types in Java

− Primitive data − Objects

  • Java variable holds either primitive value
  • r a reference to an object

16

slide-17
SLIDE 17

CS 6452: Prototyping Interactive Systems

Primitive Types

  • int, double, char, boolean

17

int total = 10; double f; char ch = 'P'; boolean done; Others exist too

slide-18
SLIDE 18

CS 6452: Prototyping Interactive Systems

Math Expressions

18

int i,j,total; j = 25; i = 10 * j + 1; sum = j * i; // Error, why? total = (i + 20) / 46.3; // Error, why?

slide-19
SLIDE 19

CS 6452: Prototyping Interactive Systems

Output

  • println – print with a newline
  • print – print with no newline

19

System.out.println("Way to go!"); System.out.println("The value is "+j+" and I'm out");

slide-20
SLIDE 20

CS 6452: Prototyping Interactive Systems

Example Program

20 public class TempConverter { //----------------------------------------------------------------- // Computes the Fahrenheit equivalent of a specific Celsius // value using the formula F = (9/5)C + 32. //----------------------------------------------------------------- public static void main (String[] args) { final int BASE = 32; final double CONVERSION_FACTOR = 9.0 / 5.0; double fahrenheitTemp; int celsiusTemp = 24; // value to convert fahrenheitTemp = celsiusTemp * CONVERSION_FACTOR + BASE; System.out.println ("Celsius Temperature: " + celsiusTemp); System.out.println ("Fahrenheit Equivalent: " + fahrenheitTemp); } }

slide-21
SLIDE 21

CS 6452: Prototyping Interactive Systems

Instantiation

  • Creating an instance (object) of a class
  • Calls a constructor method to set up object

− Has exact same name as class − Object created by new operator

21

Car c1; c1 = new Car("Ford Mustang", 2016, 322.2);

c1 Ford Mustang 2016 322.2

slide-22
SLIDE 22

CS 6452: Prototyping Interactive Systems

Access

  • We access methods through . operator
  • Let's explore provided String class

22

String j; j = new String("Hello"); int len = j.length();

slide-23
SLIDE 23

CS 6452: Prototyping Interactive Systems

String methods

23

return value char int int String String name and parameters charAt(int index) length() compareTo(String s) replace(char oldCh, char newCh) toLowerCase()

Strings are immutable (Strings are not arrays/lists of characters)

slide-24
SLIDE 24

CS 6452: Prototyping Interactive Systems

Example Program

24

public class StringMutation { public static void main (String[] args) { String phrase = "Change is inevitable"; String mutation1, mutation2, mutation3, mutation4; System.out.println ("Original string: \"" + phrase + "\""); System.out.println ("Length of string: " + phrase.length()); mutation1 = phrase.concat (", except from vending machines."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30); // Print each mutated string System.out.println ("Mutation #1: " + mutation1); System.out.println ("Mutation #2: " + mutation2); System.out.println ("Mutation #3: " + mutation3); System.out.println ("Mutation #4: " + mutation4); System.out.println ("Mutated length: " + mutation4.length()); } }

slide-25
SLIDE 25

CS 6452: Prototyping Interactive Systems

Aliasing

25

String a1 = new String("mary"); String a2 = new String("jane"); a1 = a2; // What happens?

a1 a2 mary jane

slide-26
SLIDE 26

CS 6452: Prototyping Interactive Systems

Aliasing

26

String a1 = new String("mary"); String a2 = new String("jane"); a1 = a2; // What happens?

a1 a2 mary jane

a1 and a2
 are aliases

slide-27
SLIDE 27

CS 6452: Prototyping Interactive Systems

Input

  • Java provides Scanner class

27

Scanner scan; scan = new Scanner(System.in);

Methods

String next() String nextLine() double nextDouble() int nextInt()

import java.util.Scanner; Scanner s; String reply; s = new Scanner(System.in); reply = s.nextLine(); System.out.println("Reply was "+reply);

slide-28
SLIDE 28

CS 6452: Prototyping Interactive Systems

Example Program

28 import java.util.Scanner; public class GasMileage { public static void main (String[] args) { int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } }

slide-29
SLIDE 29

CS 6452: Prototyping Interactive Systems

Informal HW

  • Get the JDK on your computer
  • Get JGrasp if you’d like to

29

Test it by running
 javac –version in a command shell

slide-30
SLIDE 30

CS 6452: Prototyping Interactive Systems

Learning Objectives

  • Java's edit-compile-run loop
  • Basics of object-oriented programming
  • Classes

− objects, instantiation, methods

  • Primitive types
  • Math expressions
  • Output, input
  • Strings

30

slide-31
SLIDE 31

CS 6452: Prototyping Interactive Systems

Next Time

  • Control flow
  • Arrays
  • Classes

− Instance data − Methods − Visibility − Inheritance

31