Java handling large numbers very strangely [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been working on a program for a class that uses mass and diameter to calculate a planet's gravity according to the equation g = (G*m)/0.5d^2. I keep getting one digit values only as opposed a mix of one and two digit values. For example: For earth I got 9.8 m/s^2, but for Jupiter (supposed to be 24.9) I got (2.49). This is what I've written thusfar.
public class PlanetGravity {
public static double fromScientificToDouble(String scientific) {
double output = Double.parseDouble(scientific.substring(0, scientific.indexOf("e")));
output *= Math.pow(10, Integer.parseInt(scientific.substring(scientific.indexOf("e") + 1)));
return output;
}
public static double calculateGravity(double d, String m) {
double output = (fromScientificToDouble("6.67e-11") * fromScientificToDouble(m));
System.out.println(output);
output = output / (Math.pow(0.0005 * d, 2));
System.out.println(output);
output = Double.parseDouble(Double.toString(output).substring(0, Double.toString(output).indexOf(".") + 3));
return output;
}
public static void printPlanet(String name, String diameter, String mass, String g) {
System.out.println(" " + name + "\t\t" + diameter + "\t\t" + mass + "\t" + g);
}
public static void main(String[] args) {
System.out.println(" \t\t\t Planetary Data");
System.out.println(" Planet\t\tDiameter (km)\tMass (kg)\tg (m/s^2)");
System.out.println("-------------------------------------------------------------------");
printPlanet("Mercury", "4880", "3.30e23 ", Double.toString(calculateGravity(4880.0, "3.30e23")));
printPlanet("Venus ", "12103.6", "4.869e24", Double.toString(calculateGravity(12103.6, "4.869e24")));
printPlanet("Earth ", "12756.3", "5.972e24", Double.toString(calculateGravity(12756.3, "5.972e24")));
printPlanet("Mars ", "6794", "6.4219e23", Double.toString(calculateGravity(6794, "6.4219e23")));
printPlanet("Jupiter", "142984", "1.900e27", Double.toString(calculateGravity(142984, "1.900e27")));
printPlanet("Saturn", "120536", "5.68e26 ", Double.toString(calculateGravity(120536, "5.68e26")));
printPlanet("Uranus", "51118", "8.683e25", Double.toString(calculateGravity(51118, "8.683e25")));
printPlanet("Neptune", "49532", "1.0247e26", Double.toString(calculateGravity(49532, "1.0247e26")));
}
}

you can simplify your codes by converting from Double to string directly, also using printf allows you to format your string (for example to 3 decimal places).
public class PlanetGravity {
public static double fromScientificToDouble(String scientific) {
//you can convert "3.30e23" directly
return Double.parseDouble(scientific);
}
public static void printPlanet(String name, String diameter, String mass, double g) {
//using printf allows you more control over the formatting like set decimal places and alignment your strings.
System.out.printf("%10s %15s %12s %8.3f \n", name, diameter, mass, g);
// System.out.println(" " + name + "\t\t" + diameter + "\t\t" + mass + "\t" + g); }
public static double calculateGravity(double d, String m) {
double output = (fromScientificToDouble("6.67e-11") * fromScientificToDouble(m));
output = output / (Math.pow(500 * d, 2));
return output;
}
public static void main(String[] args) {
System.out.println(" \t\t\t Planetary Data");
System.out.println(" Planet\t\tDiameter (km)\tMass (kg)\tg (m/s^2)");
System.out.println("-------------------------------------------------------------------");
printPlanet("Mercury", "4880", "3.30e23", calculateGravity(4880.0, "3.30e23"));
printPlanet("Venus", "12103.6", "4.869e24", calculateGravity(12103.6, "4.869e24"));
printPlanet("Earth", "12756.3", "5.972e24", calculateGravity(12756.3, "5.972e24"));
printPlanet("Mars", "6794", "6.4219e23", calculateGravity(6794, "6.4219e23"));
printPlanet("Jupiter", "142984", "1.900e27", calculateGravity(142984, "1.900e27"));
printPlanet("Saturn", "120536", "5.68e26", calculateGravity(120536, "5.68e26"));
printPlanet("Uranus", "51118", "8.683e25", calculateGravity(51118, "8.683e25"));
printPlanet("Neptune", "49532", "1.0247e26", calculateGravity(49532, "1.0247e26"));
}
}
your output will be something along the following

Related

I had to rename my files and now my runner will not function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I had to rename two files, and now they won't work when I run the demo file. I am a beginner to programming and cannot identify why my methods will not properly connect to the other file containing its constructors, methods, etc. Can someone please help me identify what is going wrong? Specifically, myCube.calcCubeArea(side) is not working, and I don't know why.
import java.util.Scanner;
public class keelanbaxter_185A01_Cubedemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the type of units: ");
String units = sc.next();
System.out.print("Enter the number of sides: ");
double side = sc.nextInt();
keelanbaxter_185A01_Cubedemo myCube1 = new keelanbaxter_185A01_Cubedemo();
myCube1.setSide(side);
System.out.println("The units you have chosen are: " + units + " and the side length is: " + side + units);
System.out.println("The area of the cube is: " + myCube1.calcCubeArea(side));
System.out.println("The volume of the cube is: " + myCube1.calcCubeVolume(side));
}
}
public class keelanbaxter_185A01_Cube {
private double side;
private static String units;
public void setSide(double S) {
side = S;
}
public double getSide() {
return side;
}
public void setUnits(String uni) {
units = uni;
}
public String getUnits() {
return units;
}
public double calcCubeVolume(double volume) {
return volume = side*side*side;
}
public double calcCubeArea(double area) {
return area = side*side;
}
}
The error is that you are not calling the correct class name, change this part:
keelanbaxter_185A01_Cube myCube1 = new keelanbaxter_185A01_Cube();
myCube1.setSide(side);

Semicolon expected in java method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
When I compile this, I get an error stating a semicolon is expected after (String[] args). I can't wrap my head around it. If I do add one, I get 10+ errors unsurprisingly
import java.util.;
import java.text.;
public class PayCheck
{
private String givenName;
private double totalWage;
private double totalHours;
private double gross, netPay, tax, ssnTax;
static String employer="PrismHR";
static double taxRate = 0.22;
static double ssRate = 0.06;
public static void main (String[] args) PayCheck()
{
Scanner std = new Scanner(System.in);
System.out.println("Enter employee name: ");
givenName = std.nextLine();
System.out.println("Enter pay rate and hours worked: ");
totalWage = std.nextDouble();
totalHours = std.nextDouble();
gross = totalWage*totalHours;
tax = gross*taxRate;
ssnTax = gross*ssRate;
netPay = gross-tax-ssnTax;
}
public PayCheck(String name, double wage, double hours)
{
this.givenName = name;
this.totalHours = hours;
this.totalWage = wage;
gross = totalWage * totalHours;
tax = gross * taxRate;
ssnTax = gross * ssRate;
netPay = gross - tax - ssnTax;
}
public String toString()
{
String emptyString = "";
NumberFormat formats = new DecimalFormat(".##");
emptyString += "Employer: " + employer + "\n";
emptyString += "Employee: " + givenName +"\n";
emptyString += "Gross income: $" + formats.format(gross)+"\n";
emptyString += "Federal Tax: $" + formats.format(tax) + "\n";
emptyString += "Social Security Tax: $" + formats.format(ssnTax) +"\n";
emptyString += "Net Pay: " + formats.format(netPay);
return emptyString;
}
}
Oftentimes, and this goes for all programming languages, the error itself may not be accurate. In your case, you have function call just kind of... there.
public static void main (String[] args) PayCheck()
{
...
}
Get rid of PayCheck(). I'm not sure what you're trying to do with it, but that is definitely causing the error.

Applying the operators on digits of number to get the desired number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Following is the google interview question, Can someone please solve or provide the logic to find the answer. Operators [+,-,*]
Output should be the list of all the possible strings which generates the target number.
Ex1:
Input = 1234
Desired number = 6
Expected Output = [2+4,3+4-1,2*3,1+2+3... etc]
Ex2:
Input = 105
Desired number = 5
Expected Output = [10-5,1*5... etc]
Here is quick fix for you.
Please check following code.
public static void main(String arg[]) {
String num = "105";
int target = 5;
getValue(num, target);
}
static void check(double sum, double previous, String digits, double target, String expr) {
if (digits.length() == 0) {
if (sum + previous == target) {
System.out.println(expr + " = " + target);
}
} else {
for (int i = 1; i <= digits.length(); i++) {
double current = Double.parseDouble(digits.substring(0, i));
String remaining = digits.substring(i);
check(sum + previous, current, remaining, target, expr + " + " + current);
check(sum, previous * current, remaining, target, expr + " * " + current);
check(sum, previous / current, remaining, target, expr + " / " + current);
check(sum + previous, -current, remaining, target, expr + " - " + current);
}
}
}
static void getValue(String digits, double target) {
for (int i = 1; i <= digits.length(); i++) {
String currentValue = digits.substring(0, i);
check(0, Double.parseDouble(currentValue), digits.substring(i), target, currentValue);
}
}
Output :
1 * 0.0 + 5.0 = 5.0
1 * 5.0 = 5.0
10 - 5.0 = 5.0
Hope this example is help you to understand the concept.

Implementing the Java comparable interface? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am not sure how to implement a comparable interface into my complex class. I have the following example code that I am using to try and get my head around it. I know it should be something like public double compareTo (Complex o) and something like that but I am not so sure on how to do it. Any suggestions on how i will implement it?:
import java.util.Scanner;
public class Complex implements Cloneable, Comparable {
private double real;
private double imag;
/*
* public Object clone() throws CloneNotSupportedException { Complex
* objClone = new Complex(); objClone.setReal(this.real);
* objClone.setImag(this.imag); return objClone; }
*/
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public Complex(double real) {
this.real = real;
}
public Complex() {
}
public void setReal(double real) {
this.real = real;
}
public void setImag(double imag) {
this.imag = imag;
}
public double getReal() {
return real;
}
public double getImag() {
return imag;
}
public void add(Complex num1, Complex num2) {
this.real = num1.real + num2.real;
this.imag = num1.imag + num2.imag;
}
public Complex subtract(Complex num) {
Complex a = this;
double real = a.real - num.real;
double imag = a.imag - num.imag;
return new Complex(real, imag);
}
public Complex multiply(Complex num) {
Complex a = this;
double real = a.real * num.real - a.imag * num.imag;
double imag = a.real * num.imag + a.imag * num.real;
return new Complex(real, imag);
}
public Complex divide(Complex c1, Complex c2) {
return new Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag),
(c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));
}
public double absolute() {
return Math.sqrt(real * real + imag * imag);
}
public String toString() {
return this.real + " + " + this.imag + "i";
}
#Override
public Complex clone() throws CloneNotSupportedException {
super.clone();
return new Complex(real, imag);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first set of complex numbers respectively: ");
double a = in.nextDouble();
double b = in.nextDouble();
Complex c1 = new Complex(a, b);
System.out.print("Enter the second set of complex numbers respectively: ");
double c = in.nextDouble();
double d = in.nextDouble();
Complex c2 = new Complex(c, d);
Complex result = new Complex(c, d);
result.add(c1, c2);
System.out.println("(" + a + " + " + b + "i) + (" + c + " + " + d + "i) = " + result.toString());
System.out.println("(" + a + " + " + b + "i) - (" + c + " + " + d + "i) = " + c1.subtract(c2));
System.out.println("(" + a + " + " + b + "i) * (" + c + " + " + d + "i) = " + c1.multiply(c2));
System.out.println("(" + a + " + " + b + "i) / (" + c + " + " + d + "i) = " + result.divide(c1, c2).toString());
System.out.println("|" + a + " + " + b + "i| = " + c1.absolute());
}
public double compareTo(Complex other) {
return this.getReal() - other.getReal();
}
}
First, the compareTo method of Comparator interface returns int, not double. Second, if you want to compare two double values in Comparator, you should never use a - b pattern. Instead, use predefined Double.compare method:
public int compareTo(Complex other) {
return Double.compare(this.getReal(), other.getReal());
}
This method carefully handles all the special values like -0.0 or NaN which are not very easy to handle manually. Note that similar methods exist for other types: Integer.compare, Long.compare and so on. It's preferred to use them.
Of course it should be noted that there's no natural order for complex numbers. Here you just compare the real parts, completely ignoring the imaginary parts.
From a mathematical standpoint, Complex numbers can't be ordered, and as such aren't a good fit for the the Comparable interface. To quote the wikipedia article:
Because complex numbers are naturally thought of as existing on a two-dimensional plane, there is no natural linear ordering on the set of complex numbers.
There is no linear ordering on the complex numbers that is compatible with addition and multiplication. Formally, we say that the complex numbers cannot have the structure of an ordered field. This is because any square in an ordered field is at least 0, but i2 = −1.
Having said that, there's nothing technically stopping you from implementing this interface. E.g., you can decide that you are sorting by the real part first and by the imaginary part second. Note that the contract of the compareTo method requires you to return an int, not a double. Also, you should define your class as extending Comparable<Complex> instead of a raw Comparable, so you don't have to mess around with casting and runtime type checking:
#Override
public int compareTo(Complex other) {
int realCompare = Double.compare(getReal(), other.getReal());
if (realCompare != 0) {
return realCompare;
}
return = Double.compare(getImag(), other.getImag());
}
EDIT:
The improvements in JDK 8's Comparator interface allow for a much more elegant implementation with the same behavior:
public int compareTo(Complex other) {
return Comparator.comparingDouble(Complex::getReal)
.thenComparingDouble(Complex::getImag)
.compare(this, other);
}
A few points worth noting.
As other answers have noted you generally should only implement Comparable if there's a natural ordering for instances of the class. As there's no natural ordering for complex numbers you likely shouldn't implement Comparable.
If you are going to provide a natural ordering then you should implement Comparable<Complex> to denote comparing to other instances of Complex (rather than comparing to other objects).
A better alternative to implementing Comparable is to provide one or more Comparator objects for your class that can be used to provide as many orderings as you want. For example:
public class Complex {
private double real;
private double imaginary;
public static final Comparator<Complex> COMPARE_BY_REAL =
Comparator.comparingDouble(Complex::getReal);
public static final Comparator<Complex> COMPARE_BY_IMAGINARY =
Comparator.comparingDouble(Complex::getImaginary);
public static final Comparator<Complex> COMPARE_BY_MODULUS =
Comparator.comparingDouble(Complex::getModulus);
private double getModulus() {
return Math.sqrt(real * real + imaginary * imaginary);
}
}
Then the user of the class can choose the ordering that makes sense to the use:
Optional<Complex> closestToOrigin = complexList.stream().min(Complex::COMPARE_BY_MODULUS);

call a method from another class

I'm in an intro programming class, in the lab that I'm currently working on we have to have two classes and pull the methods from one class, "Energy" and have them run in "Energy Driver."
I'm having trouble calling the methods (testOne, testTwo, testThree) over into "EnergyDriver"
public class EnergyDriver
{
public static void main(String [] args)
{
System.out.println(mass1 + " kiolograms, " + velocity1 +
"meters per second: Expected 61250," + " Actual " + kineticE1);
System.out.println(mass2 + " kiolograms, " + velocity2 +
"meters per second: Expected 61250," + " Actual " + kineticE2);
System.out.println(mass3 + " kiolograms, " + velocity3 +
"meters per second: Expected 61250," + " Actual " + kineticE3);
}
}
public class Energy
{
public static void main(String [] args)
{
public double testOne;
{
double mass1;
double velocity1;
double holderValue1;
double kineticE1;
mass1 = 25;
velocity1 = 70;
holderValue1 = Math.pow(velocity1, 2.0);
kineticE1 = .5 *holderValue1 * mass1;
}
public double testTwo;
{
double mass2;
double velocity2;
double holderValue2;
double kineticE2;
mass2 = 76.7;
velocity2 = 43;
holderValue2 = Math.pow(velocity2, 2.0);
kineticE2 = .5 *holderValue2 * mass2;
}
public double testThree;
{
double mass3;
double velocity3;
double holderValue3;
double kineticE3;
mass3 = 5;
velocity3 = 21;
holderValue3 = Math.pow(velocity3, 2.0);
kineticE3 = .5 *holderValue3 * mass3;
}
}
You must have only one main method in any one of class. To call a method from another class you can create an object of that class a call their respective method. Another way is by keeping the calling method to be static so you can access that method via Classname.Methodname.
public class EnergyDriver
{
public static void main(String [] args)
{
Energy energy=new Energy();
System.out.println(mass1 + " kiolograms, " + velocity1 +
"meters per second: Expected 61250," + " Actual " + energy.testOne());
System.out.println(mass2 + " kiolograms, " + velocity2 +
"meters per second: Expected 61250," + " Actual " + energy.testTwo());
System.out.println(mass3 + " kiolograms, " + velocity3 +
"meters per second: Expected 61250," + " Actual " + energy.testThree());
}
}
class Energy
{
public double testOne()
{
double mass1;
double velocity1;
double holderValue1;
double kineticE1;
mass1 = 25;
velocity1 = 70;
holderValue1 = Math.pow(velocity1, 2.0);
kineticE1 = .5 *holderValue1 * mass1;
return kineticE1;
}
public double testTwo()
{
double mass2;
double velocity2;
double holderValue2;
double kineticE2;
mass2 = 76.7;
velocity2 = 43;
holderValue2 = Math.pow(velocity2, 2.0);
kineticE2 = .5 *holderValue2 * mass2;
return kineticE2;
}
public double testThree()
{
double mass3;
double velocity3;
double holderValue3;
double kineticE3;
mass3 = 5;
velocity3 = 21;
holderValue3 = Math.pow(velocity3, 2.0);
kineticE3 = .5 *holderValue3 * mass3;
return kineticE3;
}
}
You can get the value of Kinetic Engergy 1,2,3 by using this code.
You can also use the below code which will use only one method to calculate different values by giving different arguments.
public class EngergyDriver
{
public static void main(String [] args)
{
Energy energy=new Energy();
double mass=25;
double velocity=70;
System.out.println(mass+ " kiolograms, "+velocity+"meters per second: Expected 61250," + " Actual " + energy.testOne(mass,velocity));
}
}
class Energy
{
public double testOne(double mass, double velocity)
{
double mass1;
double velocity1;
double holderValue1;
double kineticE1;
mass1 = 25;
velocity1 = 70;
holderValue1 = Math.pow(velocity1, 2.0);
kineticE1 = .5 *holderValue1 * mass1;
return kineticE1;
}
}
Java programs have SINGLE point of entry and that is through the main method.
Therefore in a single project only one class should have the main method and when compiler will look for that when you run it.
Remember that static methods cannot access non static methods hence main is static therefore it can not access testone two nor three UNLESS you create and object of that type. Meaning in the main method you can have Energy e = new Energy() then access those methods that were not declared with keyword static like e.testone() .
However take note that non static methods can access static methods through Classname.Method name because keyword static entails that only a single copy of that method/variable exists therefore we do not need an object to access it since only one copy exists.
I recommend watching the Java videos from Lynda.com or reading the books Java Head First and Java How To Program (Deitel,Deitel) to give you a boost on your Java knowledge they come with alot of exercises to enhance your knowledge.
Also there are plenty of other questions like this on SO search for them

Categories

Resources