CE00371-1 INTRODUCTION TO SOFTWARE DEVELOPMENT


Lab/Tutorial 6

Assessment weight : 3%

Due : Week 8

Programming exercises.

  1. Split the program <SwapTwoInt2.java> given in lnote27.html into two separate files: <SwapTwoInt2.java> and <IntObj.java>.

    The new <SwapTwoInt2.java> file should contains the main() and swap() methods only. And source codes of IntObj class are contained in the file <IntObj.java>. Finally compile <SwapTwoInt2.java> and <IntObj.java> separately with either

    		javac *.java 	or	javac IntObj.java
    					javac SwapTwoInt2.java
    

    Execute the result program.

    Does it work?

    What is the advantage of splitting the program and compiling the classes separately ?

  2. Given the following RedRose Applet

    import java.awt.*;
    import java.applet.Applet;
    public class RedRoseApplet extends Applet {
      public void paint( Graphics g ) {
    
        //set background color to white
        g.setColor(Color.white);
        g.fillRect(10, 10, 432, 482);
    
        //draw green sepal
        g.setColor(Color.green);
        g.fillOval(38,138,40,40);
        g.fillOval(68,138,40,40);
        g.fillOval(38,168,40,40);
        g.fillOval(68,168,40,40);
    
        //draw red petal
        g.setColor(Color.red);
        g.fillOval(28,148,90,50);
        g.fillOval(48,128,50,90);
    
        //petal outline 
        g.setColor(Color.black); 	
        g.drawArc(50,180,25,30,345,150);
        g.drawArc(75,180,25,30,160,-105);
        g.drawArc(88,163,30,20,230,-105);
        g.drawArc(70,150,30,20,290,-120);
        g.drawArc(45,140,25,30,345,-120);
        g.drawArc(30,168,30,20,50,-70);
    
        //draw pedicel
        g.setColor(Color.green);
        g.fillRect(71,218,3,100);
    
        //draw right leave
        int[] x = {73,94,131,104,73}, y = {246,216,218,250,246};
        g.fillPolygon(x,y,5);
    
        //draw left leave
        int[] x1 = {71,55,27,47,71}, y1= {268,241,245,269,268};
        g.fillPolygon(x1,y1,5);
      }
    }
    

    To draw more than one RedRose, we create a seperate RedRose class

    import java.awt.*;
    public class RedRose {
    
      //declare instance variables that are attributes of RedRose
      public Color backGroundColor = (Color.white);    
     
      public void paintRose( Graphics g,int x,int y ) {
    
        //set background color to white
        g.setColor(backGroundColor);
        g.fillRect(x+10,y+10, 150, 450);
    
        //draw green sepal
        g.setColor(Color.green);
        g.fillOval(x+38,y+138,40,40);
        g.fillOval(x+68,y+138,40,40);
        g.fillOval(x+38,y+168,40,40);
        g.fillOval(x+68,y+168,40,40);
    
        //draw red petal
        g.setColor(Color.red);
        g.fillOval(x+28,y+148,90,50);
        g.fillOval(x+48,y+128,50,90);
    
        //petal outline 
        g.setColor(Color.black); 	
        g.drawArc(x+50,y+180,25,30,345,150);
        g.drawArc(x+75,y+180,25,30,160,-105);
        g.drawArc(x+88,y+163,30,20,230,-105);
        g.drawArc(x+70,y+150,30,20,290,-120);
        g.drawArc(x+45,y+140,25,30,345,-120);
        g.drawArc(x+30,y+168,30,20,50,-70);
    
        //draw pedicel
        g.setColor(Color.green);
        g.fillRect(x+71,y+218,3,100);
    
        //draw right leaf
        int[] rightX = {x+73,x+94,x+131,x+104,x+73}, 
              rightY = {y+246,y+216,y+218,y+250,y+246};
        g.fillPolygon(rightX,rightY,5);
    
        //draw left leaf
        int[] leftX = {x+71,x+55,x+27,x+47,x+71}, 
              leftY= {y+268,y+241,y+245,y+269,y+268};
        g.fillPolygon(leftX,leftY,5);
      }
    }
    

    and use RedRoseApplet1 to draw two RedRoses

    import java.awt.*;
    import java.applet.Applet;
    public class RedRoseApplet1 extends Applet {
    
      /* declare two variables with which to instantiate 
         two RedRose object in the init() method */
      RedRose redRose1, redRose2;
    
      public void init() {
        redRose1 = new RedRose();			// create first RedRose object
        redRose2 = new RedRose();			// create second RedRose object
    
      }
    
      public void paint( Graphics g ) {
        redRose1.paintRose(g,10,10);  		// draw first RedRose
    
        redRose2.backGroundColor = Color.blue;	// use a blue background color 
        redRose2.paintRose(g,200,10);  		// draw another RedRose
      }
    }
    

    Your assignment in this exercise is to create yet another class Rose. A partially completed program is given as follow:

    import java.awt.*;
    public class Rose {
    
      //declare a constant field for green sepal
      ......
    
      //declare instance variables that are attributes of Rose
      //default color is red rose on white background
      public Color backGroundColor = Color.white; 
      
      //declare a petalColor field
      ......
    
      public void paintBackGround(Graphics g,int x,int y ) {
    
        g.setColor(backGroundColor);
        g.fillRect(x+10,y+10, 150, 450);
      } 
    
      public void paintGreenSepal(Graphics g,int x,int y ) {
        g.setColor(SEPAL_COLOR);
        g.fillOval(x+38,y+138,40,40);
        g.fillOval(x+68,y+138,40,40);
        g.fillOval(x+38,y+168,40,40);
        g.fillOval(x+68,y+168,40,40);
      }
    
      public void paintPetal(Graphics g,int x,int y ) {
    
        //draw petal
        g.setColor(petalColor);
    
        ......
      }
    
      public void paintPedicel( Graphics g,int x,int y ) {
    
        //draw pedicel
        g.setColor(Color.green);
        g.fillRect(x+71,y+218,3,100);
      }
    
      public void paintLeave(Graphics  g,int x,int y) {
    
        ......
      } 
    
      public void paintRose( Graphics g,int x,int y ) {
    
        paintBackGround(g,x,y);
        paintGreenSepal(g,x,y);
        paintPetal(g,x,y);
        paintPedicel(g,x,y); 
        paintLeave(g,x,y);
      }
    }
    

    In Rose class, you should do the following:

    Finaly, create ThreeRoseApplet to display three roses as shown below:

  3. Publish your applet after you finished.