Uploaded by stevethomas266

WOExercises06 (1)

advertisement
APCS Exposure Java Chapter 6 Worked Out Exercises Date:
1/7/20
Name:
Period:3RD
Determine the output for each program that follows.
In questions 1-6 print the exact output in the blank cell next to the program.
In questions 7-10 draw the exact output in the empty grid next to the program.
import java.text.DecimalFormat;
public class Ex0601
{
public static void main (String args[])
{
DecimalFormat output = new DecimalFormat("0000");
int x = 987;
int y = 65;
int z = 4;
System.out.println(output.format(x));
System.out.println(output.format(y));
System.out.println(output.format(z));
}
}
import java.text.DecimalFormat;
public class Ex0602
{
public static void main (String args[])
{
DecimalFormat output = new DecimalFormat("0000");
int a = 9;
int b = 8765;
int c = 432;
int d = 11;
int e = 271828;
System.out.println(output.format(a));
System.out.println(output.format(b));
System.out.println(output.format(c));
System.out.println(output.format(d));
System.out.println(output.format(e));
}
}
Exposure Java 2011, APCS Edition
0987
0065
0004
0009
8765
0432
0011
271828
WOExercises06
Page 1
08-07-11
import java.text.DecimalFormat;
public class Ex0603
{
public static void main (String args[])
{
DecimalFormat output = new DecimalFormat("0000");
int a = 35;
int b = a + 40;
int c = b * 2;
int d = a + b + c;
int e = d / 5;
System.out.println(output.format(a));
System.out.println(output.format(b));
System.out.println(output.format(c));
System.out.println(output.format(d));
System.out.println(output.format(e));
}
}
0035
0075
0150
0260
0052
import java.text.DecimalFormat;
public class Ex0604
{
public static void main (String args[])
{
DecimalFormat output = new DecimalFormat("0,000");
int a = 250;
int b = a * 20;
int c = b / 5;
int d = b – (a + c);
int e = d * 2 + 1000;
System.out.println(output.format(a));
System.out.println(output.format(b));
System.out.println(output.format(c));
System.out.println(output.format(d));
System.out.println(output.format(e));
}
}
0,250
5,000
1,000
3,750
8,500
Exposure Java 2011, APCS Edition
WOExercises06
Page 2
08-07-11
import java.text.DecimalFormat;
public class Ex0605
{
public static void main (String args[])
{
DecimalFormat money = new DecimalFormat("$0.00");
double hoursWorked = 40;
double hourlyRate = 10;
double grossPay = hoursWorked * hourlyRate;
double deductions = grossPay * 0.25;
double netPay = grossPay - deductions;
System.out.println(money.format(grossPay));
System.out.println(money.format(deductions));
System.out.println(money.format(netPay));
}
}
$400.00
$100.00
$300.00
import java.text.DecimalFormat;
public class Ex0606
{
public static void main (String args[])
{
DecimalFormat money = new DecimalFormat("$0.00");
double hoursWorked = 50;
double hourlyRate = 25;
double grossPay = hoursWorked * hourlyRate;
double deductions = grossPay * 0.2;
double netPay = grossPay - deductions;
System.out.println(money.format(grossPay));
System.out.println(money.format(deductions));
System.out.println(money.format(netPay));
}
}
$1250.00
$250.00
$1000.00
Exposure Java 2011, APCS Edition
WOExercises06
Page 3
08-07-11
import java.awt.*;
import java.applet.*;
public class Ex0607 extends Applet
{
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Polygon z = new Polygon();
z.addPoint(100,200);
z.addPoint(200,100);
z.addPoint(300,200);
z.addPoint(400,100);
z.addPoint(500,200);
z.addPoint(600,100);
z.addPoint(700,200);
z.addPoint(700,300);
z.addPoint(600,400);
z.addPoint(500,300);
z.addPoint(400,400);
z.addPoint(300,300);
z.addPoint(200,400);
z.addPoint(100,300);
g.drawPolygon(z);
}
}
800 X 500 Grid.
Each box in the grid is 100 X 100.
import java.awt.*;
import java.applet.*;
public class Ex0608 extends Applet
{
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Polygon z = new Polygon();
z.addPoint(100,200);
z.addPoint(200,100);
z.addPoint(300,200);
z.addPoint(400,100);
z.addPoint(500,200);
z.addPoint(600,100);
z.addPoint(700,200);
z.addPoint(700,300);
z.addPoint(600,400);
z.addPoint(500,300);
z.addPoint(400,400);
z.addPoint(300,300);
z.addPoint(200,400);
z.addPoint(100,300);
g.fillPolygon(z);
}
}
800 X 500 Grid.
Each box in the grid is 100 X 100.
Exposure Java 2011, APCS Edition
WOExercises06
Page 4
08-07-11
import java.awt.*;
import java.applet.*;
public class Ex0609 extends Applet
{
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Polygon z = new Polygon();
z.addPoint(100,400);
z.addPoint(200,100);
z.addPoint(300,400);
z.addPoint(400,100);
z.addPoint(500,400);
z.addPoint(600,100);
z.addPoint(700,400);
z.addPoint(100,200);
z.addPoint(700,200);
z.addPoint(100,300);
z.addPoint(700,300);
g.drawPolygon(z);
}
}
800 X 500 Grid.
Each box in the grid is 100 X 100.
import java.awt.*;
import java.applet.*;
800 X 500 Grid.
Each box in the grid is 100 X 100.
public class Ex0610 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
Font font = new
Font("Arial",Font.BOLD,36);
g.setFont(font);
g.drawString("yet?",500,400);
g.drawString("we",300,200);
g.drawString("Are",200,100);
g.drawString("done",400,300);
}
}
Exposure Java 2011, APCS Edition
WOExercises06
Page 5
08-07-11
Exposure Java 2011, APCS Edition
WOExercises06
Page 6
08-07-11
Download