Download presentation
Presentation is loading. Please wait.
Published byDerek Blaze Paul Modified over 8 years ago
1
Programming in Java Transitioning from Alice
2
Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes here }
3
Your project and classes are similar
4
Both functions and methods are referred to as methods in Java
5
void methods are methods which do not return values
6
Methods with “return types” are often referred to as functions – just as in Alice, they can return numbers (called int or double s), boolean s, String s, or Objects
7
Properties in Java are referred to as private instance variables
8
Simple Programs Very simple program to print a name Simple program to test a separate class and instantiated objects
10
Output from Play
11
Java from Eclipse main method
12
Running it Output
13
Bunny and UseBunny Create a Bunny class Properties : 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
15
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;
16
Accessor Methods -- usually functions //accessor methods public String giveColorInfo() { return color; } public int giveAgeInfo() { return age; }
17
Modifier Methods // modifier methods – change properties public void setColor (String newColor) { color = newColor; } public void setAge (int newAge) { age = newAge; }
18
UseBunny.java Revisited class 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()); }
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.