Programming in Java

19
Programming in Java Transitioning from Alice

description

Programming in Java. Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes here }. Your project and classes are similar. Both functions and methods are referred to as methods in Java. - PowerPoint PPT Presentation

Transcript of Programming in Java

Page 1: Programming in Java

Programming in Java

Transitioning from Alice

Page 2: Programming in Java

Becomes not myFirstMethod but ….

public static void main (String[] arg)

{ // code for testing classes goes here

}

Page 3: Programming in Java

Your project and classes are similar

Page 4: Programming in Java

Both functions and methods are referred to as methods in Java

Page 5: Programming in Java

void methods are methods which do not return values

Page 6: Programming in Java

Methods with “return types” are often referred to as functions – just as in Alice, they can return numbers (called int or doubles), booleans, Strings, or Objects

Page 7: Programming in Java

Properties in Java are referred to as private instance variables

Page 8: Programming in Java

Simple Programs

Very simple program to print a name

Simple program to test a separate class and instantiated objects

Page 9: Programming in Java
Page 10: Programming in Java

Output from Play

Page 11: Programming in Java

Java from Eclipse

main method

Page 12: Programming in Java

Running it

Output

Page 13: Programming in Java

Bunny and UseBunny

Create a Bunny classProperties : color and age

Modifier methods to change those properties

Accessor methods to return those properties

Special method of reach class used to create the instances – called constructor

Page 14: Programming in Java
Page 15: Programming in Java

Constructor and Instance Variables

class Bunny {

//In Java need a constructor for every new class

public Bunny() //set default properties

{

age = 0;

color = "white";

}

//private instance variables (like properties)

int age;

String color;

Page 16: Programming in Java

Accessor Methods -- usually functions

//accessor methods public String giveColorInfo() { return color; }

public int giveAgeInfo() { return age; }

Page 17: Programming in Java

Modifier Methods

// modifier methods – change properties

public void setColor (String newColor) { color = newColor; } public void setAge (int newAge) { age = newAge; }}

Page 18: Programming in Java

UseBunny.java Revisitedclass UseBunny{ public static void main(String[] arg) { Bunny b = new Bunny(); //like add Object Bunny b1 = new Bunny();

b.setAge(6); b1.setColor("green");

System.out.println("The first bunny is :"); System.out.println(b.giveAgeInfo() + " and is " + b.giveColorInfo());

System.out.println("The second bunny is :"); System.out.println(b1.giveAgeInfo() + " and is " + b1.giveColorInfo()); }}

Page 19: Programming in Java

Java Assignment

Java Assignment 1 – Part 1:Make the Lab1 project and run the FirstOne Java program

Part 2: Make the Lab1PtII project and get the UseBunny and Bunny to work