1 / 12

Objects

Objects. contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance of string s.length () calls method defined in class Most methods in String return a new string. Examples of objects.

tanith
Télécharger la présentation

Objects

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. Objects • contains data and methods • Class – type of object • Create class first • Then object or instance • String – defined class • String s; // creates instance of string • s.length() calls method defined in class • Most methods in String return a new string

  2. Examples of objects • Scanner console = new Scanner (System.in); • File f = new File(“hamlet.txt”); • Scanner input = new Scanner (f); • Scanner input = new Scanner (new File(“hamlet.txt”)); • String s1 = new String (“hello”); • Or String s1 = “hello”;

  3. > import java.awt.Point; // auto-import > Point p = new Point(3,8); > System.out.println(p); java.awt.Point[x=3,y=8] > p.translate(-1,-2); > System.out.println(p); java.awt.Point[x=2,y=6]

  4. Reference semantics • int x = 3; //primitive type • x 3 //value semantics • Point p = new Point(3,8); //object • p  x 3 y 8 //reference semantcis • P refers to the object • When working with objects, always working with reference to data rather than data itself

  5. import java.awt.Point; public class PointExamp1 { public static void main(String[] args) { Point p1 = new Point(3,8); Point p2 = new Point(); Point p3 = p2; System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); p1.x = -1; p1.y = 1; p2.x = -2; p2.y = 2; p3.x = -3; p3.y = 3; System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); } > java PointExamp1 p1 = java.awt.Point[x=3,y=8] p2 = java.awt.Point[x=0,y=0] p3 = java.awt.Point[x=0,y=0] p1 = java.awt.Point[x=-1,y=1] p2 = java.awt.Point[x=-3,y=3] p3 = java.awt.Point[x=-3,y=3]

  6. Multiple objects • Point p1 = new Point(3,8); // separate instance • Point p2 = new Point(); //separate instance • Point p3 = p2; // points to p2

  7. Objects as parameters to Methods public static void manipulate (Point p) { p.x = 99; p.y = 99; } Point p1 = new Point(3,3); manipulate(p1);

  8. import java.awt.Point; public class PointExamp1 { public static void main(String[] args) { Point p1 = new Point(3,8); Point p2 = new Point(5,2); silly(p1); silly(p2); Point p3 = p1; silly(p3); System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); } public static void silly (Point p) { int temp = p.x; p.x = p.y; p.y = temp; } }

  9. Evaluate the following • What are the x and y coordinates of the Points p1,p2, and p3 after the following? Point p1 = new Point(17,9); Point p2 = new Point(4,-1); Point p3 = p2; p1.translate(3,1); p2x = 50; p3.translate(-4,5)

  10. public class StringComparision1 { public static void main(String[] args) { String name1 = "Bob"; String name2 = new String("Bob"); String name3 = "Bob"; // 1st case if (name1 == name2) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } // 2nd case if (name1 == name3) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } }

  11. public class Temperature { public static void main(String[] args) { double tempf = 98.6; double tempc= 0.0; ftoc(tempf,tempc); System.out.println(“Body temp in C is: “ + tempc); } public static void ftoc(double tempf, double tempc) { tempc = (tempf – 32) * 5/ 9; } }

  12. Evaluate the following • Math.abs(-1.6) • Math.pow(6,2) • Math.max(7,4) • Math.min(-2,-5) • Math.sqrt(64) • Math.abs(2 + -4) • Math.sqrt(16) * Math.max(Math.abs(-5),Math.abs(-3))

More Related