Output: The Object's Position After 10.0 Seconds Is - 490.50000000000006 M

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

package GravityCalculator;

public class GravityCalculator {

public static void main(String[] arguments) {

double gravity = -9.81; // Earth's gravity in m/s^2

double initialVelocity = 0.0;

double fallingTime = 10.0;

double initialPosition = 0.0;

double finalPosition = 0.0;

finalPosition = 0.5*gravity*fallingTime*fallingTime+initialVelocity*fallingTime + initialPosition;

System.out.println("The object's position after " + fallingTime +

" seconds is " + finalPosition + " m.");

//OUTPUT OF UNMODIFIED PROGRAM

//The object's position after 10.0 seconds is 0.0 m.

OUTPUT:

The object's position after 10.0 seconds is -490.50000000000006 m.

package FoodCorporation;

import java.util.*;

public class FoodCorporation

static Scanner console = new Scanner(System.in);

public static void main(String[] args)

{
double tpay;

double[] hours={0,35,47,73};

double[] bpay={0,7.50,8.20,10.00};

//Employee1

if (hours[1] > 40.0)

tpay = 40.0 * bpay[1] + 1.5 * bpay[1] * (hours[1] - 40.0);

else

tpay = hours[1] * bpay[1];

System.out.printf("Total Pay for Employee 1 are $%.2f %n",tpay);

System.out.println();

//Employee2

if (hours[2] > 40.0)

tpay = 40.0 * bpay[2] + 1.5 * bpay[2] * (hours[2] - 40.0);

else

tpay = hours[2] * bpay[2];

System.out.printf("Total Pay for Employee 2 are $%.2f %n",tpay);

System.out.println();

//Employee3

if (hours[3] > 40.0)

tpay = 40.0 * bpay[3] + 1.5 * bpay[3] * (hours[3] - 40.0);

else

tpay = hours[3] * bpay[3];

System.out.printf("Total Pay for Employee 3 are $%.2f %n",tpay);

System.out.println();
}

OUTPUT:

Total Pay for Employee 1 are $262.50

Total Pay for Employee 2 are $414.10

Total Pay for Employee 3 are $895.00

package Marathon;

class Marathon {

public static int findFastest(int[] times)

int fastestTime = times[0];

int indexOfFastest = 0;

int counter = 1;

int time = 0;

while(counter < times.length)

time = times[counter];

if(time < fastestTime)

fastestTime = time;

indexOfFastest = counter;

counter += 1;
}

return indexOfFastest;

public static int findSecondFastest(int[] times)

int indexOfSecondFastest = 0;

int secondFastestTime = times[0];

int indexOfFastest = findFastest(times);

int counter = 1;

int time = 0;

while(counter < times.length)

if(counter == indexOfFastest)

counter += 1;

continue;

time = times[counter];

if(time < secondFastestTime)

secondFastestTime = time;

indexOfSecondFastest = counter;

counter += 1;

}
return indexOfSecondFastest;

public static void main (String[] arguments) {

String[] names = {

"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",

"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",

"Aaron", "Kate"};

int[] times = {

341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,

343, 317, 265};

int indexOfFastest = findFastest(times);

System.out.println("The fastest person was " + names[indexOfFastest] + " with a time of " +
times[indexOfFastest] + " minutes.");

int indexOfSecondFastest = findSecondFastest(times);

System.out.println("The second fastest person was " + names[indexOfSecondFastest] + " with a time of "
+ times[indexOfSecondFastest] + " minutes.");

OUTPUT:

The fastest person was John with a time of 243 minutes.

The second fastest person was Kate with a time of 265 minutes.

public class Book {


String title;
boolean borrowed;
public Book(String bookTitle) {
}
public void borrowed() {
}
public void returned() {
}
public boolean isBorrowed() {
}
public String getTitle() {
}
public static void main(String[] arguments) {
Book myBook = new Book("Da Vinci");
myBook.name = "The Da Vinci Code"
System.out.println("Title (should be The Da Vinci Code): " +
example.getTitle());
System.out.println("Borrowed? (should be false): " +
example.isBorrowed());
example.rented();
System.out.println("Borrowed? (should be true): " +
example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " +
example.isBorrowed());
}
}

package library;

public class Library {


public static void main(String[] args) {
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();II Return The Lords of the Rings to the
first library
System.out.println("Returning The Lord of the Rings:");
firstlibrary.returnBook("The Lord of the Rings");
System.out.println();
II Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstlibrary.printAvailableBooks();
}
}

OUTPUT:
Book.java
Title (should be The Da Vinci Code): The Da Vinci Code
Rented? (should be false): false
Rented? (should be true): true
Rented? (should be false): false

Library.java
Library hours:
Libraries are open daily from 9am to 5pm.
Library addresses:
10 Main St.
228 Liberty St.
Borrowing The Lord of the Rings:
You successfully borrowed The Lord of the Rings
Sorry, this book is already borrowed.
Sorry, this book is not in our catalog.
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
You successfully returned The Lord of the Rings
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel implements Runnable {


private static final long serialVersionUID =
-7469734580960165754L;
private boolean animate = true;
private final int FRAME_DELAY = 50; // 50 ms = 20 FPS
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
private DrawGraphics draw;

public SimpleDraw(DrawGraphics drawer) {


this.draw = drawer;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
draw.draw(g2);
}
public synchronized void start() {
animate = true;
}

public synchronized void stop() {


animate = false;
}
private synchronized boolean animationEnabled() {
return animate;
}

public void run() {


while (true) {
if (animationEnabled()) {
repaint(); }
try {
Thread.sleep(FRAME_DELAY);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

public static void main(String args[]) {


final SimpleDraw content = new SimpleDraw(new DrawGraphics());

JFrame frame = new JFrame("Graphics!");


Color bgColor = Color.white;
frame.setBackground(bgColor);
content.setBackground(bgColor);
// content.setSize(WIDTH, HEIGHT);
// content.setMinimumSize(new Dimension(WIDTH, HEIGHT));
content.setPreferredSize(new Dimension(WIDTH, HEIGHT));
// frame.setSize(WIDTH, HEIGHT);
frame.setContentPane(content);
frame.setResizable(false);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0);
}
public void windowDeiconified(WindowEvent e)
{ content.start(); }
public void windowIconified(WindowEvent e)
{ content.stop(); }
});
new Thread(content).start();
frame.setVisible(true);
}
}

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class BouncingBox {


int x;
int y;
Color color;
int xDirection = 0;
int yDirection = 0;

final int SIZE = 20;

/**
* Initialize a new box with its center located at (startX,
startY), filled
* with startColor.
*/
public BouncingBox(int startX, int startY, Color startColor) {
x = startX;
y = startY;
color = startColor;
}

/** Draws the box at its current position on to surface. */


public void draw(Graphics surface) {
// Draw the object
surface.setColor(color);
surface.fillRect(x - SIZE/2, y - SIZE/2, SIZE, SIZE);
surface.setColor(Color.BLACK);
((Graphics2D) surface).setStroke(new BasicStroke(3.0f));
surface.drawRect(x - SIZE/2, y - SIZE/2, SIZE, SIZE);

// Move the center of the object each time we draw it


x += xDirection;
y += yDirection;

// If we have hit the edge and are moving in the wrong


direction, reverse direction
// We check the direction because if a box is placed near the
wall, we would get "stuck"
// rather than moving in the right direction
if ((x - SIZE/2 <= 0 && xDirection < 0) ||
(x + SIZE/2 >= SimpleDraw.WIDTH && xDirection > 0)) {
xDirection = -xDirection;
}
if ((y - SIZE/2 <= 0 && yDirection < 0) ||
(y + SIZE/2 >= SimpleDraw.HEIGHT && yDirection > 0)) {
yDirection = -yDirection;
}
}

public void setMovementVector(int xIncrement, int yIncrement) {


xDirection = xIncrement;
yDirection = yIncrement;
}
}
import java.awt.Color;

import java.awt.Graphics;

public class DrawGraphics {

BouncingBox box;

java.util.ArrayList<BouncingBox> listOfBoxes;

/** Initializes this class for drawing. */

public DrawGraphics() {

listOfBoxes = new java.util.ArrayList<BouncingBox>();

listOfBoxes.add(new BouncingBox(200, 50, Color.RED));

listOfBoxes.add(new BouncingBox(100, 75, Color.BLUE));

listOfBoxes.add(new BouncingBox(200, 10, Color.GREEN));

box = listOfBoxes.get(0);

box.setMovementVector(0, 1);

listOfBoxes.get(1).setMovementVector(1, 0);

listOfBoxes.get(2).setMovementVector(1, 1);

/** Draw the contents of the window on surface. Called 20 times per second. */

public void draw(Graphics surface) {

surface.drawLine(50, 50, 250, 250);

surface.drawOval(200, 200, 50, 100);

surface.draw3DRect(100, 150, 25, 50, true);

surface.drawString("Foo!", 100, 100);


for (BouncingBox curBox: listOfBoxes) {

curBox.draw(surface);

OUTPUT:

You might also like