Calculate PI Using Monte Carlos Method

Aug 10, 2014

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.

Here is the link to the original post ...

View Replies


ADVERTISEMENT

Recursive Method - Calculate Greatest Common Divisor Using Euclidean Method

Apr 29, 2014

Consider the following recursive method that calculates the greatest common divisor using Euclidean method.

PHP Code:

public static int GCD ( int x , int y )
{
    if ( y == 0 )                        
        return x;
    else if ( x >= y && y > 0)
        return GCD ( y , x % y );
    else return GCD ( y , x );  


Trace the above method for x=32 and y=46

View Replies View Related

Trying To Calculate Fine Using Method Calls

Jul 3, 2014

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...

View Replies View Related

How To Calculate Remove Method Complexity

Feb 1, 2015

I need to figure out how to calculate the remove method complexity in the worst best and average case same as the insert method

public void insert(String Word)
{
size++;
int pos = myhash(Word);
WordNode nptr = new WordNode(Word,null);
if (table[pos] == null)
table[pos] = nptr;

[Code] .....

View Replies View Related

How To Add A Method To Calculate Mean Of Length Of Each Word Output

Mar 6, 2014

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!");

[Code] .....

View Replies View Related

Java Program Method To Calculate Distance

Feb 20, 2015

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.

View Replies View Related

Method From Main - Calculate Weight On Different Planets

Nov 8, 2014

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)

View Replies View Related

Write A Program That Will Call A Method To Calculate Function

Dec 3, 2014

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:

x f(x)
1 -15
2 -12
3 -7
4 0
5 9
6 20
7 33
8 48
9 65
10 84

I guess it also has to print this output in a table even though my professor never mentioned it. Usually

public class Lab12 {
public static int f(int x)
{
return x*x-16;

[code]....

View Replies View Related

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;

[code]....

View Replies View Related

Loop That Calculate Distance Traveled - Why Java Not Printing Method Results

Apr 13, 2014

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();
 
[Code] .....

View Replies View Related

Method Parameters - Two Fields Of Type Double / Calculate Distance To Another Point

Oct 18, 2014

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).

double distanceTo(Point otherPoint) {
double distanceX = getDistance(this.x, otherPoint.x);
double distanceY = getDistance(this.y, otherPoint.y);
return calculateLength(distanceX, distanceY);
}

View Replies View Related

MVC Calculator - How To Get It To Actually Calculate

Sep 28, 2014

I made a MVC calculator. I was wondering if you could take a look at my design and if I was on track. I am still working on getting it to actually calculate something. All the buttons respond and print text on the JTextField but it is not calculating.

package calculator.MVC;
import javax.swing.JButton;
import javax.swing.JTextField;
public class CalculatorModel {
private int sum;
private int number;
private char opt;

[code]....

View Replies View Related

Calculate (C) How Is The Result 3

Apr 3, 2013

public class NewClass4 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);

View Replies View Related

Calculate Radius Of A Pond?

May 31, 2014

This is the code

public class PondRadius {
public static void main (string[] args) {
//Calculate the radius of a pond
//which can hold 20 fish averaging 10 inches long
int fishCount = 20;//number of fish in pond
int fishLength = 10;// Average fish length
int inchesPerFoot = 12;//number of inches in one foot

[Code] ....

And here is the error or exceptions i received

C:javaLesson1>javac PondRadius.java
PondRadius.java:23: error: ')' expected
System.out.println("To hold" + fishCount + fish averaging " + fishLength +" inch
es long you need a pond with an area of
" +

[Code] .....

View Replies View Related

Calculate Lowest Value Of Array

Feb 13, 2015

So I have this code to calculate the lowest value of an array:

public class Exercise1 {
public static void main(String[] args) {
int[] theArray = {1/*/,2,3,4,5,6,7,8,9,10/*/};
int result = Exercise1.min(theArray);
System.out.println("The minimum value is: " +result);

[code].....

and I need to write an exception class that should be thrown if the array does not hold any elements.

View Replies View Related

How To Calculate Difference Between Two Dates

Oct 19, 2014

I have been tasked with creating an invoice (school assignment). Part of the calculations is creating an interest depending on the amount of days between the current date entered, and invoice date entered (The program prompts the user to enter both the current and invoice dates by asking for the day, month and year).

We are also supposed to set the contructor to a default date of 1/1/1900.. but I don't know how or where to code that.

How to calculate the difference between the CurrentDate and Invoice. I have displayed the dates to the user as follows.

public void displayDate() {
System.out.println("
Today's Date: " + month + "/" + day + "/" + year);
}
public void displayInvDate() {
System.out.println("
Invoice Date: " + invMonth + "/" + invDay + "/" + invYear);

View Replies View Related

How Does Program Know To Calculate For Only One Year

Mar 26, 2015

how does the program know to calculate for only one year?

public class Interest2 {
public static void main(String[] args) {
double principal; // The value of the investment.
double rate; // The annual interest rate.
double interest; // The interest earned during the year.

[code]....

View Replies View Related

Calculate Minimum And The Maximum

Nov 21, 2014

i need to calculate the minimum and the maximum, actually it seems to be easy but, the minimum should be the smallest number but 0..this is my code

Java Code:

Scanner s = new Scanner (System.in);
int max = 0 ;
int min = 0 ;
System.out.println(" Please enter 3-5 numbers");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
int d = s.nextInt();
int e = s.nextInt();

[code]....

View Replies View Related

How To Calculate Parking Rates

Mar 31, 2015

there are the normal parking rates with two condition using two difference code and two difference calculator method. the fees for the two code also different.i need to display the code, enter time. enter out, and parking charge. i have a problem with the processor method.

View Replies View Related

Calculate Sum Of All Integers Between 1 And Given Integer N

Aug 3, 2014

So I wrote a method that simply calculates the sum of all integers between 1 and a given integer n. The method works fine however, as n gets big the solution will have time and space problems. Some I'm just curious if there is a better method than my iterative one that would produce a better Big O value.

public static int sum(int n)
{
int total = 0;
for (int i = 1; i <=n; i++)
total += i;
return total;
}

View Replies View Related

Calculate Percent In Java

Jan 3, 2014

I would like to change the precent from *1.17 (17%) to calc of precent like this :

3% + 5% + 5$

For example :

The resault is 10$ - i want to get :

10$ +3%(0.3$) + 0.5% (0.51$) + 5$ = 15.81$

How i change this line to this calc

if(results[i].getSite()=='Xxx'&&XPrice==0)
{
XPrice =(results[i].getPrice() *1.17);
}
}

View Replies View Related

Using For Loop To Calculate Power?

Oct 16, 2014

I'm new to java and i'm trying to figure out how to calculate power in java using the for loop.

The geometric sequence is (d*c^(n-1)).

So it goes d, d*c^1, d*c^2,......., d*c^(n-1)

??????for (i=1; i<=n; i++){

result

}????????

How to program this. how do i write this using for loop?

View Replies View Related

Calculate Value Of PI Using Series Expansion

Jun 18, 2014

Write a program called ComputePi to compute the value of pi using the following series expansion. you have to decide on the termination criterion used in the computation(such as number of terms used or the magnitude of an additional term). Is this series suitable for compute pi?

PI=4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)+.......)

View Replies View Related

Calculate If Code Matches

Jan 19, 2015

I have for example a letter code "EECC"...Now i want to have a good loop that checkes if the code typed in by the player matches or not? For example if player types CECE it should say it matches because C occurs 2 times and E occurs 2 times. This function must return than 4.If player types CEEE, it must return 3, because the code has 2 C en 2 E, player input is 1 C and 3 E, the last E is wrong.I've tried some loops with breaks but they all fail..

for example:

String test = "EFCC";
String test3 = "ECEC";
for(int i = 0; i < test.length(); i++){
matchFound = false;

[Code] .....

This returns 2 instead of 3.

View Replies View Related

Find Min And Max Values And Calculate Mean For Data Set

May 2, 2014

The first is MyArray Class (You can either use an array or an arrayList)Create a class called LastNameFirstNameMyArray according to the UML diagram. This class will allow a user to enter 5 integer values into an array(or object of ArrayList<Integer>). It contains methods to find min and max values and calculate the mean for the data set.

LastNameFirstNameMyArray - data[ ] : int (or data : ArrayList<Integer> )
- min : int
- max: int
- mean : double + LastNameFirstNameMyArray()
+ findMin() : void
+ findMax() : void
+ calculateMean(): void
+ toString() : String

Attributes:
data[]the array which will contain the values or data<Integer>the arraylist which will contain the values
minthe minimum value among the values in the arraylist
maxthe maximum value among the values in the arraylist
mean the arithmetic average of the values

Methods:
You need to implement the constructor LastNameFirstNameMyArray(), which populates 5 values. In addition, the constructor should call findMin() and findMax(), and also calculateMean().

Methods:
LastNameFirstNameMyArray()the constructor. It will allocate memory for the array (or arraylist) of size 5. Use a for loop to repeatedly display a prompt for the user which should indicate that user should enter value 1, value 2, etc. Note: The computer starts counting with 0, but people start counting with 1, and your prompt should account for this. For example, when the user enters value 1, it will be stored in indexed variable 0. The constructor should populate 5 integer values, call findMin() and findMax(), and also calculateMean().

findMin()this is a method that uses a for loop to access each data value in the array (or arraylist) and finds the minimum value. The minimum value is stored into min.

findMax()this is a method that uses a for loop to access each data value in the array (or arraylist) and finds the maximum value. The maximum value is stored into max.

calculateMean()this is a method that uses a for loop to access each data value in array (or arraylist) and add it to a running total. The total divided by the number of values (use the length of the array), and the result is stored into mean.
toString()returns a String containing data, min, max, and the mean.

Task #2 TestMyArray

1. Create a LastNameFirstNameTestMyArray class. This class only contains the main method. The main method should declare and instantiate a LastNameFirstNameMyArray object.
2. Compile, debug, and run the program. Then, it should print out the contents, the min, max, and mean of the data.

and the second is A theater seating chart is implemented as two-dimensional array of ticket prices, like this:

10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
20 20 30 30 40 40 30 30 20 20
20 30 30 40 50 50 40 30 30 20
30 40 50 50 50 50 50 50 40 30

Write a program that prompts the users to pick either a seat or price. Mark sold seats by changing the price the 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price.

Note: Assume that if the user enters 1 for the row and 1 for the seat, the yellow marked sit will be sold.

View Replies View Related

Calculate Coupon Discount In Java?

Feb 9, 2015

i finished up a project here and seem to be running into some issues. I figured its my notation, when i ask a user to enter a number, if its below 10, it should *only* print "No Coupon." but what appears to happen is that the program prints "No Coupon" Plus the "discount coupon and % of purchase."

import java.util.Scanner;
public class assign1c { /* Start of Class 1c */
static double cost;
static double coupon = 0;
static String x = "No coupon";
public static void main(String[] args) {
// TODO Auto-generated method stub

[code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved