Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.

Similar presentations


Presentation on theme: "CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on."— Presentation transcript:

1 CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on paper in class. Late rules in effect - late programs are reduced in value by half. Wednesday office hours 4->6 have been cancelled for this week, have been replaced with hours on Friday, 3-5.

2 Objects and Methods Methods can be passed objects: L.drawOn(d); Methods can return objects: public String doubleString(String s){return (s + s); } Constructors make objects The new keyword, + a constructor call, mfgs an object that can be copied to a variable (or returned from a method) Pt p = new Pt(3,45); Constructors give the rules for manufacture; “new” activates the construction work

3 Static variables and static methods Static variables don’t belong to an object -- they only belong to the class. Constants are sometimes designated static, since their values are independent of the characteristics of a calling object. Static methods are methods that aren’t tied to objects. main is one! also: Math.sqrt(), etc

4 Wrapper classes These classes shadow the primitive types, e.g. Integer for int, Character for char, etc They represent an awkward response to Java’s “currency” issue: the main currency is objects, but objects are a pain if you’re working with - say - ints. These classes come with some very useful static methods. String s = “236”; int n = Integer.parseInt(s); // n now holds num 236

5 Nested classes Classes are our mechanism for organizing data -- so it makes senses for us to have subsidiary, embedded or nested classes to capture subsidiary relationships. Such relationships can actually happen in a number of ways - we’ll look at one: nonstatic inner classes. Our example: We’ll make a wheel class. The wheel will roll across the screen. We’ll endow it with spokes. The spokes don’t make any sense except in the context of the Wheel class - so we’ll make it an inner class.

6 Interfaces Interfaces are a very big deal. The interface is Java’s specification mechanism. They support encapsulation. Think of copy and paste in a text editor. Think of the actions you use when you drive a car. You’re only interested in effects, not in the “how” of it. Interfaces represent a contract

7 An interface is a “class” with a list of constants and method headers public interface Drawable{ // in elements; 11 headers public int left(); public void center(); public void drawOn(DrawingWindow d);.. public class Rect implements Drawable{ Blah blah.. }

8 import element.*; public class WheelTester{ public static void main(String[] args){ int width = 500; int height = 200; DrawingWindow d = new DrawingWindow(width,height); Wheel w = new Wheel(100,20,width,height); w.roll(d); } }

9 import element.*; public class Wheel{ final int VERTICAL = 0; final int HORIZONTAL = 1; int slant = VERTICAL; int base; // floor of roll int size; // radius of wheel int xPos,yPos; // wheel ctr Circle c; Spoke s; int width, height;

10 // the Wheel constructor public Wheel(int base, int size,int width, int height){ this.base = base; this.size = size; c = new Circle(0,base,size); this.width = width; this.height = height; xPos = 0; yPos = base; s = new Spoke(slant); }

11 public void drawOn(DrawingWindow d){ c.drawOn(d); s.drawOn(d); } public void clearOn(DrawingWindow d){ c.clearOn(d); s.clearOn(d); } public void move(int dx, int dy){ xPos += dx; yPos += dy; c.move(dx,dy); s.move(dx,dy); }

12 public void roll(DrawingWindow d){ Waiting w = new Waiting(); for(int j = 0; j < width; j++){ move(1,0); drawOn(d); w.snooze(40); clearOn(d); } }

13 private class Spoke{ public int slant; public Pt top,bot; public Line L; Spoke(int slant){ this.slant = slant; if (slant == VERTICAL){ top = new Pt(xPos,yPos - size); bot = new Pt(xPos, yPos + size); } else { top = new Pt(xPos+size,yPos); bot = new Pt(xPos-size, yPos); } }

14 public void move(){ if(slant ==VERTICAL) slant = HORIZONTAL; // change the slant else slant = VERTICAL; if (slant == VERTICAL){ top = new Pt(xPos,yPos - size); bot = new Pt(xPos, yPos + size); } else { top = new Pt(xPos+size,yPos); bot = new Pt(xPos-size, yPos); } L = new Line(top,bot); }

15 public void drawOn(DrawingWindow d){ L.drawOn(d); } public void clearOn(DrawingWindow d){ L.clearOn(d); } }


Download ppt "CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on."

Similar presentations


Ads by Google