The Law Code of Hammurabi, Summaries of Law

We can surmise that the code was produced in several copies and distributed throughout Hammurabi's domains and we can be certain that is was a public document, ...

Typology: Summaries

2021/2022

Uploaded on 07/04/2022

Bjarne_90
Bjarne_90 šŸ‡³šŸ‡“

4.9

(8)

337 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Session 7
Methods
Strings
Constructors
this
Inheritance
Java Methods
•Methods are the interface or
communications between classes
–They provide a way to invoke the same
operation from many places in your program,
avoiding code repetition
–They hide implementation details from the
caller or user of the method
–Variables defined within a method are not
visible to callers of the method; they have local
scope within the method.
–The method cannot see variables in its caller
either. There is logical separation between the
two, which avoids variable name conflicts.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download The Law Code of Hammurabi and more Summaries Law in PDF only on Docsity!

Session 7

Methods

Strings

Constructors

this

Inheritance

Java Methods

• Methods are the interface or

communications between classes

  • They provide a way to invoke the same

operation from many places in your program,

avoiding code repetition

  • They hide implementation details from the

caller or user of the method

  • Variables defined within a method are not

visible to callers of the method; they have local

scope within the method.

  • The method cannot see variables in its caller

either. There is logical separation between the

two, which avoids variable name conflicts.

Invoking a Method

public class Power1 { public static void main(String[] args) { double a= 10.0, b; int x= 3; System.out.println("Main: a= " + a + ", x= " + x); b= power(a, x); System.out.println("Main: Result b= " + b); }

public static double power(double d, int i){ System.out.println("Power: d= " + d + ", i= " + i); double x= d; // Different x than main x for (int j= 1; j < i; j++) x *= d; // x = x * d; return x; } }

Passing Arguments

void main(String[ ] args){…

double a;

int x;

b= power(a, x);

double power(double d, int i)

{ // Method makes own copy

// of d and i implicitly

double x= d;

return x;}

Return value Argument 1 Argument 2

Assume

method is

written first:

can’t know

main() vars

Communi-

cation only

via arg list,

return value

Arguments

matched by

position

Call By Value Example

public class CallByValue { public static void main(String[] args) { int i= 3; double d= 77.0; System.out.println("Main 1: i= " + i + ", d= " + d); triple(i, d); // No return value System.out.println("Main 2: i= " + i + ", d= " + d); // Secondary part of example: argument conversion triple(i, i); // Ok-Java converts int to double System.out.println("Main 3: i= " + i); } public static void triple(int ii, double dd) { System.out.println(ā€œTriple 1: ii= " +ii+ ", dd= " +dd); ii = 3; // ii= ii3; dd *= 3.0; System.out.println(ā€œTriple 2: ii= " +ii+ ", dd= " +dd); } }

Call By Value With Object

public class CallObjExample { public static void main(String[] args) { Demo d= new Demo(3); int i= 3; System.out.println("Main1: i= " + i + ", d.a= " + d.a); triple(i, d); // No return value System.out.println("Main2: i= " + i + ", d.a= " + d.a); } public static void triple(int ii, Demo dd){ System.out.println("T1: ii= "+ ii + ", dd.a= " + dd.a); ii = 3; // ii= ii3; dd.a *= 3; System.out.println("T2: ii= "+ ii + ", dd.a= " + dd.a); } }

Call By Value With Object

class Demo {

// public only to show call by value

public int a;

public Demo(int aa) {

a= aa;

Call by Value With Object

Demo d= new Demo(3);

int i= 3;

triple(i, d);

ii *= 3;

dd.a *=3;

d= Demo

i=^3

Main Triple

dd=

ii= 39

public class Overload { public static void main(String[] args) { System.out.println("First version=ā€œ+ retVal("1.5E3") ); System.out.println("Second version=" + retVal(1500) ); System.out.println("Third version=" + retVal(1.5, 3.0) ); }

public static double retVal(String s) { return Double.parseDouble(s);}

public static double retVal(int i) { return (double) i; // cast optional here }

public static double retVal(double m, double exp){ return m * Math.pow(10.0, exp); } }

String class

• Part of Java system

• String class has special operator for

concatenation

• String class allows constant

initializer, as in

String testString = ā€œHelloā€;

String is not modifiable once

created-ā€immutableā€

Strings

public class StringExample { public static void main(String[] args) { String first= "George "; String middle= "H.W. "; String last= "Bush"; String full= first + middle + last; System.out.println(full);

// Testing for equality in strings (objects in general) String full2= "George H.W. Bush"; if (full.equals(full2)) // Right way System.out.println("Strings equal"); if (full == full2) // Wrong way System.out.println("A miracle!"); if (first == "George ") // Wrong way, but works System.out.println("Not a miracle!");

Strings

// Modifying strings must be done indirectly

// Strings are constant

middle = middle.substring(0, 2) + " ";

full= first + middle + last;

System.out.println(full);

ā€œthisā€- How an object refer

to itself

• Objects are accessed by variables

we called references.

• Suppose from code within an object,

we want to refer back to the object.

• Example: Suppose we want to pass a

reference to ā€œourselfā€ as argument to

a method of another object

public Spring(String m, double len, double md, double k) { material= m; length= len; maxDeflect= md; this.k= k; // ā€œthisā€ } public Spring(double len, double k) { this("steel", len, 0.25*len, k); // ā€œthisā€ }

two=

Spring

Spring(ā€œsteelā€, 5, 1.25, 3.0)

this

ā€œthisā€ is also used as shorthand to refer to

other constructors for the class

two = new Spring(5.0, 3.0)

this in a constructor

public class SimplePoint { private double x, y; // Data members public SimplePoint() { // Constructor x= 0.0; y= 0.0; } public SimplePoint(int x, int y) { this.x = x; this.y = y; } // Methods public double getX() { return x;} public double getY() { return y;} public void setX(double xval) { x= xval;} public void setY(double yval) { y= yval;} public void move(double deltaX, double deltaY) { x += deltaX; y += deltaY; } } // End of class SimplePoint

Springs

F= k dx

Spring main()

public class SpringExample { public static void main(String[] args) { Spring one= new Spring("aluminum", 2.0, 1.0, 5.0); Spring two= new Spring(5.0, 3.0); Spring three= new Spring(); // 3 diff constructors

double f1= one.getForce(0.5); double f2= two.getForce(1.5); double f3= three.getForce(0.1); System.out.println("f1: " + f1 + "\nf2: " + f2 + "\nf3: " + f3);

double f4= one.getForce(); // Overloaded methods double f5= two.getForce(); double f6= three.getForce(); System.out.println("f4: " + f4 + "\nf5: " + f5 + "\nf6: " + f6); System.exit(0); } }

Spring Class Design

• All the Spring methods are public

  • Any method of any class can call these methods
  • Private methods can be used, as helpers or for

tricky things that only the class itself should do

• Data fields in Spring are private

  • Only Spring methods can access them
  • Public data fields are almost never used

• Constructor name must be same as class

name

  • Constructors are called with new only
  • Constructors cannot have a return value

Spring Class Methods

• Why have all these methods?

  • Get methods are only access from other classes

to Spring data

  • Allow us to reimplement Spring if we need
  • Set methods should do error checking
  • Our method should make sure that length>0, max

deflection < length, etc.

• Overloaded methods must have different

signatures (arguments)

  • Different return types cannot distinguish two

otherwise identical methods

  • int getForce(int a) and
  • double getForce(int b) would not compile

Object Destruction

• Java reclaims object memory automatic-

ally using ā€˜garbage collection’ when there

are no active references to the object

  • C++ requires the programmer to do this

manually. You use ā€˜new’ sparingly in C++

because you have to use ā€˜delete’ when done,

to avoid ā€˜memory leaks’

• Java has finalizers to clean up other

resources (files, devices locked, etc.)

when an object is destroyed

  • Informal advice: never use finalizers
  • They can invoke any object, so garbage

collector can be wildly inefficient

Beam Exercise, p.

• Optional, advanced:

– Add dimensional analysis:

  • Store the units for each variable in the class
    • Decide how you’ll code them ( exponents, etc.)
  • Modify constructors to take unit arguments
  • Convert units if needed (N – lbf, m – ft)
    • 1 lbf= 4.4 N, 1 ft= 0.3 m
  • Make sure units match in the calculation
  • Output the units with the method result

Beam Class

class Beam { private double L; private double E= 30000.0; // Initialization private double I= 1000.0; public Beam(double L, double E, double I) { this.L= L; // this to access object fields this.E= E; this.I= I; } public Beam(double L) { this.L= L; // Rely on initialization for others } // Could use this(L, 30000., 1000.); public double getDeflection(double P) { return -PLLL/(3.0EI); } public double getDeflection(double P, String u) { if (u.equals("ft")) // not == return -3.3PLLL/(3.0EI); else return -PLLL/(3.0EI); } }

Beam Main()

public class BeamExample { public static void main(String[] args) { Beam one= new Beam(20.0); double w1= one.getDeflection(1200.0); System.out.println("w1: " + w1); double w2= one.getDeflection(1200.0, "ft"); System.out.println("w2: " + w2); System.exit(0); } }