1 / 19

Programming in Java

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.

tave
Download Presentation

Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 doubles), booleans, Strings, 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

  9. Output from Play

  10. Java from Eclipse main method

  11. Running it Output

  12. 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

  13. 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; Constructor and Instance Variables

  14. Accessor Methods -- usually functions //accessor methods public String giveColorInfo() { return color; } public int giveAgeInfo() { return age; }

  15. Modifier Methods // modifier methods – change properties public void setColor (String newColor) { color = newColor; } public void setAge (int newAge) { age = newAge; } }

  16. 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()); } }

  17. 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

More Related