100 likes | 112 Views
This laboratory study assigns a Java programming task to calculate the distance traveled by a car on a tank of gas and convert currency amounts. It also includes examples of drawing objects using the java.awt package.
E N D
Laboratory Study II October, 24 2003
Java Programming Assignment • Write a program to calculate and output the distance traveled by a car on a tank of gas. • The user should interactively input figures for the capacity of the tank and average rate of gas consumption per mile by the car. • Write a program to convert any amount of Turkish Liras input by the user into any chosen world currency. • Write the conversion factor for the currency of your choice as a constant. • Write a program to convert a temperature that is interactively entered by the user in degrees Fahrenheit to degrees Celsius. • The formula for conversion is: Celsius = (Fahrenheit - 32) * (5/9).
import java.awt.*; import java.applet.Applet; public class Applet1 extends Applet { public void paint(Graphics g){ //draw triangle g.drawLine(20,20,20,100); g.drawLine(20,20,100,60); g.drawLine(100,60,20,100); }//end paint }//end triangle
One of the classes in the java.awt package is class Graphics.Graphics objects contain many subroutines Three of them are as follows: g.setColor(c): is called to set the color that is used for drawing. • The parameter, c is an object belonging to a class named Color, another one of the classes in the java.awt package. • About a dozen standard colors are available as static member variables in the Color class. • These standard colors include Color.black, Color.white, Color.red, Color.green, and Color.blue. • For example, if we want to draw in red, you would say "g.setColor(Color.red);". • The specified color is used for all drawing operations up until the next time setColor is called.
g.drawRect(x,y,w,h) draws the outline of a rectangle. • The parameters x, y, w, and h must be integers. • This draws the outline of the rectangle whose top-left corner is x pixels from the left edge of the applet and y pixels down from the top of the applet. The width of the rectangle is w pixels, and the height is h pixels. • g.fillRect(x,y,w,h) is similar to drawRect except that it fills in the inside of the rectangle instead of just drawing an outline. • drawString(String str, int x, int y) draws the text given by the string str. • The string is drawn using the current color and font of the graphics context. x specifies the position of the left end of the string. y is the y-coordinate of the baseline of the string. The baseline is a horizontal line on which the characters rest.
import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics g) { g.drawString(“Zeynep", 25, 60); g.drawString (“Ali", 25, 85); g.drawString (“Fatma", 25, 105); g.setColor(Color.green); g.fillRect(30, 40, 100, 15); g.setColor(Color.blue); g.fillRect(30, 65, 135, 15); g.setColor(Color.yellow); g.fillRect(30, 90, 170, 15); } } Exercise on this applet changing the borders of methods
import java.awt.*; import java.applet.Applet; public class Applet3 extends Applet{ public void paint(Graphics g){ //write name labels g.drawString(“Zeynep",0,50); g.drawString(“Ali",0,75); g.drawString(“Okul",0,100); g.setColor(Color.blue); //draw and label name Zeynep g.fillRect(15,25,150,20); g.drawString(“first winner",184,50); g.setColor(Color.green); //draw and label Ali g.fillRect(15,65,175,20); g.drawString(“secod winner",194,75); g.setColor(Color.red); //draw and label name Fatma g.fillRect(15,105,200,20); g.drawString(“third winner",204,100);}}
Compare applets Applet2 and Applet3 • Exercise on applets Applet3 changing the borders of methods
Another applet example defining and using objects import java.awt.*; import java.awt.event.*; import java.applet.*; public class Rectangle extends Applet{ public Rectangle (int x1, int y1, int x2, int y2){ this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public Rectangle() { System.out.println (“Drawing Rectangle”); x1=10; y1=10; x2=10; y2=10; } public int x1, y1, x2, y2; }