Introduction to Java E177 April 29, me. berkeley

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Procedural programming in Java Methods, parameters and return values.
CSC 142 Computer Science II Zhen Jiang West Chester University
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Creating Web Services Presented by Ashraf Memon Hands-on Ghulam Memon, Longjiang Ding.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
SEEM Java – Basic Introduction, Classes and Objects.
CSE 1201 Object Oriented Programming ArrayList 1.
Converting string to integer class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Classes - Intermediate
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Object Oriented Programming Lecture 2: BallWorld.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CMSC 202 ArrayList Aug 9, 2007.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Arrays 3/4 By Pius Nyaanga.
Introduction to Modular Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Conditional Execution
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Computer Programming Methodology Input and While Loop
Methods.
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
An Introduction to Java – Part I
CMSC 202 Static Methods.
Starting Out with Java: From Control Structures through Objects
Chapter 8: Collections: Arrays
Pass by Reference, const, readonly, struct
Class Info E177 January 22, me. berkeley
CSC 113 Tutorial QUIZ I.
An Introduction to Java – Part I, language basics
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
CMSC 202 ArrayList Aug 9, 2007.
Classes and Objects 5th Lecture
CHAPTER 6 GENERAL-PURPOSE METHODS
@ReferAssignGetSet February 21, me. berkeley
CMSC 202 ArrayList Aug 9, 2007.
JAVA Constructors.
Object Oriented Programming
Classes and Objects Static Methods
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Introduction to java Part I By Shenglan Zhang.
More on iterations using
Presentation transcript:

Introduction to Java E177 April 29, 2008 http://jagger. me. berkeley Introduction to Java E177 April 29, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-8, Andy Packard, Trent Russi. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

A simple application (StringFormatDemo.java) This application illustrates the. public class StringFormatDemo { public static void main (String[] args) { String s1, s2; double A; int B; A = 14.2; B = -9; s1 = String.format("B=%d, A=%g, sqrt(A)=%g",B,A,Math.sqrt(A)); s2 = String.format("Length of s1 = %d",s1.length()); System.out.println(s1); System.out.println(s2); } Method(s) for the StringFormatDemo class Here there is only one method, called main. Java applications begin executing at main. This method returns nothing (void) and has an array of String as input arguments. Direct call to “format”method of String class Format specifiers Direct call to “sqrt”method of Math class The “standard output” object PrintLine method Input arg to method

Compiling it In this simple environment, compile using the javac command Run using the java command.

A simple program (StringFormatDemo2.java) This application illustrates the. public class StringFormatDemo2 { public static void main (String[] args) { String s1, s2, s3; double A = 14.2; int B=-9, i, L1; s1 = String.format("B=%d, A=%g, sqrt(A)=%g",B,A,Math.sqrt(A)); L1 = s1.length(); s3 = new String("23rd character in s1 is " + s1.charAt(22)); System.out.println("Length of s1 = " + L1); System.out.println(s1); for (i=1;i<=L1;i++) { s2 = String.format("%d",i%10); System.out.print(s2); } System.out.print("\n"); System.out.println(s3); Method of String class Object of class String Input arg to method Same idea here, just no input arguments to length method Recall in Matlab, the object is an explicit input argument to method... L1 = length(s1) charAt(s1,22)

A simple program (BeerSong.java) This application illustrates the. While loop public class BeerSong { public static void main (String[] args) { int beerNum = 99; String sword = "bottle"; String pword = "bottles"; String uword; if (beerNum>1) {uword = pword;} else { uword = sword;} while (beerNum > 0) { System.out.println(beerNum + " " + uword + " of beer on the wall"); System.out.println(beerNum + " " + uword + " of beer."); System.out.println("Take one down."); System.out.println("Pass it around."); beerNum = beerNum - 1; if (beerNum > 0) { System.out.println(beerNum + " " + uword + " of beer on the wall."); } else { System.out.println("No more " + pword + " of beer on the wall."); } System.out.println(" "); String concatenator

Input Args to main (BeerSongControl.java) This application illustrates how input arguments to main arise, and are used. public class BeerSongControl { public static void main (String[] args) { int beerNum; String sword, pword, uword; beerNum = Integer.valueOf(args[2]).intValue(); sword = args[0]; pword = args[1]; if (beerNum>1) {uword = pword;} else { uword = sword;} while (beerNum > 0) { System.out.println(beerNum + " " + uword + " of beer on the wall"); System.out.println(beerNum + " " + uword + " of beer."); System.out.println("Take one down."); System.out.println("Pass it around."); beerNum = beerNum - 1; if (beerNum > 0) { System.out.println(beerNum + " " + uword + " of beer on the wall."); } else { System.out.println("No more " + pword + " of beer on the wall."); } System.out.println(" ");

Input Args to main (BeerSongControl.java) beerNum = Integer.valueOf(args[2]).intValue(); Integer is a wrapper class for the primitive data type int. Similarly Double is a wrapper class for the primitive data type double. intValue method of Integer class. Extracts the primitive int value from the Integer object 3rd input argument to main, object of class String Direct call to Integer class valueOf method, which converts a String into an object of class Integer Object of class Integer

Input Args to main (FPAdder.java) This application illustrates how input arguments to main arise, and are intepreted as doubles. public class FPAdder { public static void main(String[] args) { int i = 0; double num = 0; if (args.length <= 1) { System.out.println("You need to specify at least two numbers."); } else { while (i < args.length) { num += Double.valueOf(args[i]).doubleValue(); i++; } System.out.println("The sum of the numbers is: " + num); For now, every array knows its length.

primitives (referenceTest.java) Primitive data types are referred to by value. public class referenceTest { public static void main (String[] args) { int A, B; A = 4; B = A; /* copy */ System.out.println(String.format("Initial: A=%d, B=%d",A,B)); System.out.println("Change B's value to 9"); B = 9; System.out.println(String.format("Final: A=%d, B=%d",A,B)); }

Arrays of primitives (referenceTest2.java) Arrays (even of primitives) are objects, and are “by reference”. public class referenceTest2 { public static void main (String[] args) { int[] myInts, myInts2; myInts = new int[4]; myInts[0] = 8; myInts[1] = 6; myInts[2] = 4; myInts[3] = 2; myInts2 = myInts; System.out.println(String.format("Init: mI[0]=%d, mI2[0]=%d",myInts[0],myInts2[0])); System.out.println("Change myInts2[0]'s value to 88"); myInts2[0] = 88; System.out.println(String.format("Final: mI[0]=%d, mI2[0]=%d",myInts[0],myInts2[0])); }

primitives (referenceTest3.java) Passing arguments to a method public class referenceTest3 { public static void main(String[] args) { int[] myIntArr = new int[1]; int myInt = 8; myIntArr[0] = 8; referenceTest3.myTest(myIntArr,myInt); System.out.println(myIntArr[0]); System.out.println(myInt); } public static void myTest(int[] a, int b) a[0] = 9; b = 9;

primitives (LoanClass.java) This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} public double getPrinciple() { return L } public double[] getBalance() { } public void setPrinciple(double Lnew) { update(); } private void update() { if (dprop.equals("Principle")) { L = getL(R,M,N); } } private double getL(double R, double M, int N) { /* formula */ } The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method getPrinciple method. Note that methods have access to the instance variables.

(LoanClass.java) public class LoanClass { private double L; This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} public double getPrinciple() { return L; } public double getInterestRate() { return R*100; } public double getMonthlyPayment() { return M; } public int getDuration() { return N; } public String getDerivedProperty() { return dprop; } The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method getPrinciple method. Note that methods have access to the instance variables.

(LoanClass.java) public class LoanClass { private double L; This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} public double[] getBalance() { double b[] = new double[N+1]; int i; double a = (1+R/12); b[0] = L; for(i=1;i<N;i++) { b[i] = b[i-1]*a - M; } b[N] = 0; return b; getPrinciple method. Note that methods have access to the instance variables. The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method

(LoanClass.java) public class LoanClass { private double L; This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} public void setPrinciple(double Lnew) { if (!dprop.equals("Principle")) { L = Lnew; } else { System.out.println("Principle is the DerivedProperty "); } update(); getPrinciple method. Note that methods have access to the instance variables. The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method

(LoanClass.java) public class LoanClass { private double L; This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} private void update() { if (dprop.equals("Principle")) { L = getL(R,M,N); } else if (dprop.equals("InterestRate")) { R = getR(L,M,N); } else if (dprop.equals("MonthlyPayment")) { M = getM(L,R,N); } else if (dprop.equals("Duration")) { N = getN(L,R,M); } getPrinciple method. Note that methods have access to the instance variables. The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method

(LoanClass.java) public class LoanClass { private double L; This illustrates a class defintion and methods public class LoanClass { private double L; private double R; private double M; private int N; private String dprop; public LoanClass(String d) { L=1;R=0;M=1;N=1;dprop = d;} private double getL(double R, double M, int N) { double out; double a = (1+R/12); if (a==1) { out = M*N; } else { out = M*(Math.pow(a,N)-1)/(Math.pow(a,N)*(a-1)); } return out; getPrinciple method. Note that methods have access to the instance variables. The instance variables. In Matlab, these would be the fields of the underlying struct object. Here, they are declared private. Only the methods have access to these variables. In Matlab, this “privacy” is automatic – only the methods have access to the fields of the struct. Constructor method

Using LoanClass.java (loandemo.java) public class loandemo { public static void main(String[] args) { LoanClass A = new LoanClass("MonthlyPayment"); LoanClass B = new LoanClass("Principle"); System.out.println("AProp = " + A.getDerivedProperty()); System.out.println("APrinc = " + A.getPrinciple()); A.setPrinciple(30000); A.setInterestRate(9); A.setDuration(36); System.out.println("AMonP = " + A.getMonthlyPayment()); }