Core Java

Fundamental Programming Structures in Java

Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.

.jpg

Copyright © Cay S. Horstmann 2016

Write a simple Java program

We will not use ‘Hello, World!’

Method Calls

Comments

FirstSample

Work with numeric data types

Integers

Floating-Point Numbers

The char Type

The boolean Type

Variables

Mathematical Operators and Functions

Type Conversions

More Operators

Enumerated Types

Java 9 News Flash - JShell

.png

Cursor Keys and Tab Completion

JShell Tips

Java 10 News Flash - Local Type Inference

.png

Work with Strings and the API documentation

Strings

String Concatenation

More About Strings

Code Points and Code Units

The String API

Java 9 News Flash - API Search

.png

Write programs that read input and produce output

Console Input

Formatted Output

File Input and Output

InputTest

Use the control flow constructs of the Java language

The if Statement

.gif

if (yourSales >= 2 * target)
{
   performance = "Excellent";
   bonus = 1000;
}
else if (yourSales >= 1.5 * target)
{
   performance = "Fine";
   bonus = 500;
}
else if (yourSales >= target)
{
   performance = "Satisfactory";
   bonus = 100;
}
else
{
   System.out.println("You're fired");
}

The while Statement

.gif

while (balance < goal)
{
   balance += payment;
   double interest = balance * interestRate / 100;
   balance += interest;
   years++;
}
System.out.println(years + " years.");

The do/while Statement

.gif

do
{
   balance += payment;
   double interest = balance * interestRate / 100;
   balance += interest;
   year++;
   // print current balance
   . . .
   // ask if ready to retire and get input
   . . .
}
while (input.equals("N"));

Retirement

Retirement2

The for Statement

.gif

for (int i = 1; i <= 10; i++)
   System.out.println(i);

LotteryOdds

The switch Statement

.gif

Scanner in = new Scanner(System.in);
System.out.print("Select an option (1, 2, 3, 4) ");
int choice = in.nextInt();
switch (choice)
{
   case 1:
      . . .
      break;
   case 2:
      . . .
      break;
   case 3:
      . . .
      break;
   case 4:
      . . .
      break;
   default:
      // bad input
      . . .
      break;
}

The break Statement

The continue Statement

Work with big numbers when arbitrary precision is required

Big Numbers

BigIntegerTest

Java 9 News Flash - BigInteger.TWO

.png

Use arrays to store multiple elements of the same type

Arrays

Supplying Element Values

Copying Arrays

LotteryDrawing

Multidimensional Arrays

CompoundInterest

LotteryArray