int i = 0; while ( i > 0 ) i--;
for ( int i = 0; i >= 0; i--) System.out.println();
int j = 0; for ( int i = 0; i >= 0; i--) { int j = i; }
public void mystery(int n) { if ( n < 10 ) System.out.print(n); else { mystery(n/10); System.out.print(n%10); } }
for (int i=5 ; i > 0 ; i--) { Circle c = new Circle(new Point(i,0),i); c.draw(); }
for (int i=2 ; i < 6; i= i+2) { Line l = new Line(new Point(-i,i), new Point(i,i)); l.draw(); }
int i=0; while (i < 4) { for (int j=0; j <= i ; j++) { if ( (i+j)%2 == 0 ) System.out.print(i+j); else System.out.print("*"); } System.out.println(); i++; }Output:
0 *2 2*4 *4*6
class Gems { public void agate() { n++; } M public void emerald(String s) { t = s; } M public Gems() { n = 0; t = "";} C public int topaz() { return n; } A public Gems(int a, String s) { n = a; t = s; } C public void diamond() { System.out.println(t); } A public String ruby() { return t; } A public Gems(int a) { n = a;t = "";} C private int n; private String t; }
public class Mystery { public static void main(String[] arg) { Gems mine = new Gems(); Gems yours = new Gems(5, "precious"); mine.emerald("industrial"); mine.diamond(); System.out.println("\n" + yours.topaz()); yours.emerald(mine.ruby()); yours.diamond(); } }Output:
industrial 5 industrial
public void turquois() { if ( t.length() < 10 ) n = n/2; else n = 3*n + 1; }
A Trojan horse is a program that allows access to an already-penetrated system-- for example by establishing a new account with superuser privileges. A logic bomb is a program that is triggered to act upon detecting a certain sequence of events-- for example a program written by a disgruntled employee that destroys system files when the employee is removed from the system. A virus is a a self-replicating program that causes damage and infects other programs, floppy disks, or hard disks.
Would it be ethical to look at the files? Should you notify the owner, the instructor, the system adminstrator? What is the ethical thing to do, with respect to your ethical view? Please state the ethical framework and justify your answer.
Match the javadoc comments below:
/** * calculates the numeric grade, given the letter grade. * @param grade the letter grade * @return the numeric grade or -1 if the input is invalid */ public int convert(String grade) { if ( grade.length() != 1 ) return -1; else if ( grade.equals("A") ) return 4; else if ( grade.equals("B") ) return 3; else if ( grade.equals("C") ) return 2; else if ( grade.equals("D") ) return 1; else if ( grade.equals("F") ) return 0; else return -1; }
// This program converts letter grades to numeric grades. #import ccj.* public class GradeConvert { public static void main(String[] arg) { // Ask user for letter grade: System.out.print("Please enter letter grade: "); String letterGrade = Console.in.readWord(); // Print out number grade: System.out.println("\nThe number grade is " + convert(letterGrade)); } }
/** * Draws a box given the upper left and lower right corner * @param ul the upper left corner * @param lr the lower right corner */ public void drawBox(Point ul, Point lr) { Point ur = new Point(lr.getX(), ul.getY()); Point ll = new Point(ul.getX(), lr.getY()); new Line(ll,ul).draw(); new Line(ul,ur).draw(); new Line(ur,lr).draw(); new Line(lr,ll).draw(); }
import ccj.* public class BarChart extends GraphicsApplet { public void run() { int numBars = readInt("Enter number of bars:"); setCoord(0,100,numBars,0); for (int i = 0 ; i < numBars ; i++) { int height = readInt("Enter height:"); drawBox(new Point(i, height), new Point(i+1,0)); } } }
<applet code="BarChart.class" height=300 width=300 > </applet>
import ccj.*; public class Rectangle { /** * Constructs a square of length 2 centered at (0,0). */ public Rectangle() { ul = new Point(-1,1); lr = new Point(1,-1); } /** * Constructs a rectangle given two opposite corners.. * @param p1 upper left corner * @param p2 lower right corner */ public Rectangle(Point p1,Point p2) { ul = p1; lr = p2; } /** * Draws rectangle on the graphics window. */ public void draw() { Point ll = new Point(ul.getX(), lr.getY()); Point ur = new Point(lr.getX(), ul.getY()); new Line(ll,ul).draw(); new Line(ul,ur).draw(); new Line(ur,lr).draw(); new Line(lr,ll).draw(); } /** * Moves rectangle x units in the horizontal direction and * y units in the vertical direction. * @param x horizontal change * @param y vertical change */ public void move(double x, double y) { ul.move(x,y); lr.move(x,y); } /* Include variables to hold the data: */ Point ul; Point lr; }
public void again(Point p1, Point p2, boolean change) { if ( distance(p1, p2) < 1 ) return; Point q1 = new Point ( p1.getX(), p2.getY() ); Point q2 = new Point ( p2.getX(), p1.getY() ); new Line(p1,q1).draw(); new Line(q1,p2).draw(); new Line(p2,q2).draw(); new Line(q2,p1).draw(); if ( change ) { Point m1 = midpoint(p1,q1); Point m2 = midpoint(p2,q2); new Line(m1,m2).draw(); again(m1, p2, !change); } else { Point m1 = midpoint(p1,q2); Point m2 = midpoint(p2,q1); new Line(m1,m2).draw(); again(p1, m2, !change); } } public double distance(Point a, Point b) { return ( Math.sqrt( (a.getX() - b.getX())*(a.getX() - b.getX()) + (a.getY() - b.getY())*(a.getY() - b.getY()))); } public Point midpoint(Point a, Point b) { double x = (a.getX() + b.getX())/2; double y = (a.getY() + b.getY())/2; return new Point(x,y); }
again(new Point(0,0), new Point(1,-1), true)Output:
again(new Point(0,2), new Point(4,0), false)Output:
public void nestedCircles(Circle c) { double rad = c.getRadius(); if ( rad <= 1 ) return; Point p = c.getCenter(); // Set up and draw left circle: Point lp = new Point(p.getX() - rad/2, p.getY()); nestedCircles(new Circle(lp, rad/2); // Set up and draw right circle: Point rp = new Point(p.getX() + rad/2, p.getY()); nestedCircles(new Circle(rp, rad/2); }
When a customer rents or returns a snowboard, the shop updates the number of boards of that kind that are rented. The shop also occasionally retires old snowboards or buys new ones, so, the shop needs to be able to update the number of boards of each kind it owns.
Assume that the shop has three different kinds of snowboards: those made by Burton, those made by Ride, and those made by Oxygen. Initially, they have 10 of each.
I would use two classes: Snowboad and Shop. Snowboard would keep track of the manufacturer, the total number of board owned, and the number of boards currently rented. Shop would have 3 member variables for each of the different kind of snowboards: Burton, Ride, and Oxygen.
Snowboard: I would have 3 accessor funtions to get the number rented, the total stock, and the kind: getNumRented() getTotalStock() getKind() I would also have 3 mutator functions to change the values of the member variables: setNumRented() setTotalStock() setKind() There would also be 2 constructors: the default, and one that takes a string and 2 integers: Snowboard(String newKind, int newTotal, int newNumRented) Shop: I would have two accessor functions that takes the kind of the board. The first returns the total stock of that kind. The second returns the number rented of that kind: getTotal() getRented() There would be two mutator functions-- one that updates the number of boards rented (given the kind) and the other updates the total number of boards: updateRented() updateTotal() There would also be a default constructor.
First, there should be simple tests that make sure the information is entered correctly. Next, you should write tests to make sure the number of boards owned or rented is not negative. You should also test that you do not rent more boards than you own, and that you do not retire (that is, remove from the system) boards that are currently rented to a customer.