Modify DrawShape Method To Calculate Distance From Starting Point
Mar 13, 2015
I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.
Java Code:
public class PiTurtle extends Turtle
{
private double mySize;
private int mySides;
private double diameter = 0;
I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.
Here is the code:
public class PiTurtle extends Turtle { private double mySize; private int mySides; private double diameter = 0; final static double startX = 590.0; final double startY; public PiTurtle(int nSides)
I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x).
Write method distance, which calculates the distance between two points (x1, y1) and (x2, y2). All numbers and returned values should be of type double. Incorporate this method into an program that enable the user to enter the coordinates of the points, then calculate and display the distance by calling the method –distance.
I've tried numerous times to make it work and I'm on the right path, however I'm missing some things in the code to make my results look like this later on, which I've attached onto this post.
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Hours Traveled "); int hoursTraveled = input.nextInt(); System.out.println("Enter MPH "); int mph = input.nextInt();
I am working on a simple app that should emulate a desktop with some icons that you can drag around. I was instructed to use the MouseListener and MouseMotionListener. The icons should drag starting at the point where the mouse originally clicked down on it (ie not dragging from the top right corner).
I am drawing my icons to a jpanel, and I seem to be having two problems:The hitboxes are offThe icon is being dragged from the top right corner, despite having setup up a mouse offset at the beginning of the drag..Here are the relevant classes, the Desktop.draw() method is being called from my DrawPanel
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Desktop implements MouseListener, MouseMotionListener{ static DesktopIcon[] icons;
Pretty much what im trying to accomplish, i need to write a program that figures out the distance between one point to another, using miles and feet..
Heres how it has to look: "The distance from my uncles house is ___ miles, or ____ feet."
I can get it to run if i add only whole miles..but when i try to add 8.5 miles, and compile, the program flips out..I know i need to use a double somewhere, but cant figure it out, here is my code..
import java.util.Scanner; //required for input public class feetToMiles { public static void main (String[] args){ //Create new scanner object called input Scanner input = new Scanner (System.in); //allows for input
Basically I'm looking for a way to make one object follow another. For example, if I move object A to one area of the screen I want object B to to move to object A's location but I also want object B to move at a fixed speed (movement variable). How do I go about doing this?
Both the x and y coordinates of object B would need to know the coordinates of object A to calculate the distance between the two and to determine how much of which axis to increment/decrement (if that makes sense?) with the inclusion of the speed variable. In other words I'm just trying to create a homing object.
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
Write a java program that calculate the grade point of three subjects and their credit hours through if the grade points and the credit hours are
Subject---grade---gradepoint-----credit hours Maths------A--------4.0---------3 English composition-----B-------3.0-----2 French-----B+ ---- 3.3----------3
Now,if the grade point is equal to 4 ,print out First class If the grade point is equal or greater than 3.0 but less than 4,print out Second class upper If the grade point is equal or greater than 2.0 but less than 3,print out second class lower Use the if-else-if statement
I was given some code by a professor to add some features to as part of an assignment. However, the code itself doesn't seem to work.
import java.util.HashSet; import java.util.InputMismatchException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; public class DijkstraPriorityQueue
[Code] ....
The method to find minimum distance is nonfunctional...I receive an error that the types are incompatible. I can't do the assignment if the base code doesn't work to begin with...
Modify class Time2 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time2 object should always remain
a) incrementing into the next minute,
b) incrementing into the next hour and
c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
how to manage case 4 stuff and what's the problem of this CODE.
import java.util.Scanner; public class Time2Test { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); Time2 time = new Time2(); // input System.out.println( "Enter the time" ); System.out.print( "Hours: " ); time.setHour( input.nextInt() );
I'm trying to call the grade.processFile method from the main method but I'm getting this Error below. I'll post my code which includes the main method and the class underneath the error message:
Exception in thread "main" java.lang.NullPointerException at java.io.FileInputStream.<init>(FileInputStream.jav a:130) at java.util.Scanner.<init>(Scanner.java:611) at MyGrades.processFile(MyGrades.java:49) at myGradesMain.main(myGradesMain.java:19) import java.util.Scanner; import java.io.*;
I was reading the oracle java tutorial under: URL....Here's the code for the Point class:
public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } }
and in the Rectangle class you have the following constructor:
public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h;
If we create a new Point object like this:
Point originOne = new Point(23, 94);
and then a new Rectangle object like this:
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Will that set originOne to point to the object Point at (23, 94). just want to make that this is the meaning of this statement: Point(Point p)Constructs and initializes a point with the same location as the specified Point object.
I am trying to calculate a fine in a PoliceOfficer object with method calls to a ParkedCar and ParkingMeter object. The word problem is:
The fine is $25 for the first hour or part of it and $20 for every additional hour of part of it.
My code is:
public class PoliceOfficer public static final int PARKING_FINE1 = 25; public static final int PARKING_FINE2 = 20; public static final int NUMBER_OF_MINUTES_OVER_PARKED = 60; public double calculateFine(){ double calculateFine = 0;
[Code] ....
obviously the fine is not calculated correctly but I'm not sure how to proceed from here...
I've been looking at the calculating PI using Monte Carlos method. All of the codes request an input from the user to be able to determine PI. However, I want the program to print out only 10, 100, 1000, 10000, 100,000 and 1,000,000,000 for PI.
import java.awt.*; import java.applet.*; import java.awt.event.*; public class Assignment extends Applet implements ActionListener { TextArea textInput = new TextArea(); // user input Button analyzebutton = new Button("Analyze"); Button resetbutton = new Button("Reset"); Label lbloutput = new Label ("Please enter text into the textbox!");
I have to write a program to calculate my weight on different planets and to do that, I have to read in the surface gravity of all the planets from a file using a separate method from main
public static double[] readGravity() throws IOException { double[] surfaceGravity = new double[8]; Scanner readFile = new Scanner("gravity1.txt"); int i = 0; while (readFile.hasNext()) { surfaceGravity[i] = readFile.nextDouble(); i++; } return surfaceGravity; }
I get this message in the while loop when I try to run the program:
java.util.InputMismatchException; null (in java.util.Scanner)
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
I just went through some multiple choice questions and found this question with the below choices,
a) It is not standardized b) To avoid conflicts since assemblers and loaders use such names c) To avoid conflicts since library routines use such names d) To avoid conflicts with environment variables of an operating system
Now , what should be the explanation for answering such question.
I want to find a certain element in array I have to scan through the array index starting from 0 until I find the number I am looking for. Even in data structures which use hashing like HashMap and Hashtable we will have to scan through the keys until we find the key we are looking for. So what is the use of hashing over index based searching? I mean how is hashing an advantage over an array?