Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java E177 April 29, me. berkeley

Similar presentations


Presentation on theme: "Introduction to Java E177 April 29, me. berkeley"— Presentation transcript:

1 Introduction to Java E177 April 29, 2008 http://jagger. me. berkeley
Introduction to Java E177 April 29, Copyright , Andy Packard, Trent Russi. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

2 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

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

4 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)

5 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

6 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(" ");

7 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

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

9 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)); }

10 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])); }

11 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;

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

13 (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.

14 (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

15 (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

16 (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

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

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


Download ppt "Introduction to Java E177 April 29, me. berkeley"

Similar presentations


Ads by Google