









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 17
This page cannot be seen from the preview
Don't miss anything!










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; } }
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); } }
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); } }
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); } }
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!");
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ā }
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
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); } }
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); } }
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); } }