Below is my code,
// Testing class circle
import java.text.DecimalFormat ;
import javax.swing.JOptionPane ;
public class CircleTest {
public static void main( String args[] )
{
// instantiate Circle object
Circle circle = new Circle( ) ;
Point3 point = new Point3( 40, 50 ) ;
// get circle's initial x - y coordinate and radius
String output = "\nX coordinate is " + circle.getX( ) +
"\nY coordinate is " + circle.getY( ) +
"\nRadius is " + circle.getRadius() ;
circle.setX( 35 ); // set new x - coordinate
circle.setY( 20 ); // set new y - coordinate
circle.setRadius( 4.25 ); // set new radius
// get String representation of new circle value
output += "\n\nThe new location and radius of circle are\n" +
circle.toString() ;
// format floating - point values with 2 digits of precision
DecimalFormat twoDigits = new DecimalFormat ( " 0.00 ") ;
// get Circle's diameter, Circumference and area respectively
output += "\nDiameter is " + twoDigits.format( circle.getDiameter() ) +
"\nCircumference is " + twoDigits.format( circle.getCircumference() ) +
"\nArea is " + twoDigits.format( circle.getArea( ) +
" Test sum is: " + twoDigits.format( point.sum( ) ) ) ;
JOptionPane.showMessageDialog( null, output ) ;
System.exit( 0 );
} // end method main
} // end class CircleTest
I am getting an error as
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:507)
at java.text.Format.format(Format.java:157)
at CircleTest.main(CircleTest.java:35)
However I am not getting the above error if I append the output separately with
output += " Test sum is: " + twoDigits.format( point.sum() ) ;
Why is it so? What is the problem ?
You are applying format at the whole string message, not just the area: a ) is missing, there.
You are mistakenly put the bracket.
Here is the fixed one
output += "\nDiameter is " + twoDigits.format(circle.getDiameter()) +
"\nCircumference is " + twoDigits.format(circle.getCircumference()) +
"\nArea is " + twoDigits.format(circle.getArea()) +
" Test sum is: " + twoDigits.format(point.sum()) ;
Related
How should I write this function. Argument in is in [2,2^60] and exp in [2,60].
QUEST1 : How to avoid the MESSY test ? I could not make it with Big decimal.This is like an exponential version of the old classic bug ( 1 multiply by 3 then divided by 3 is not always 1).
QUEST2 : How to code it if in is much bigger ( 2^90)
See code below. If needed I can post the code that test it all.
/** #param in input
* #param exp exponent >=2
* #return floor[in^(1/exp)] */
public static long floorOk1( final long in , final int exp) {
final long ret = (long) Math.floor(Math.pow(in,1.0/exp));
if ( Math.pow(ret+1,exp) <= in ) CORRECTIONS++; // add 1 needed
return ( Math.pow(ret+1,exp) <=in ) ? ret+1L: ret ;
}
// All OK floorBAD(25 ,2)=5 = floorULP(25 ,2)
// floorULP(125,3)=5 OK ,BUT floorBAD(125,3)=4 (must be 5 as 5^3=125)
// floorULP(24389,3)=28 and floorBAD(24389,3)=28 BOTH WRONG (should be 29 as 29^3= 24389 )
/** (24389,3)->28 is WRONG should be 29 , BUT ( 125/3)=5 ok */
public static long floorULP( final long in , final int exp) {
final double val = Math.pow(in,1.0/exp) ;
return (long) ( Math.floor(val+Math.ulp(val)));
}
/** Arguments (125/3) and (24389,3) -> WRONG results: 1 less than expected. */
public static long floorBAD( final long in , final int exp) { return (long) Math.floor(Math.pow(in,1.0/exp)); }
/** Args(in,exp)= (125 ,3) ==> floorBAD is wrong but floorULP is OK
* Args(in,exp)= (24389,3) ==> floorBAD is wrong AND floorULP also WRONG
* NOTE : WE HAVE 5^3= 125 ; 29^3= 24389 **/
public static void smallDifferenceTest() {
System.out.println(" floorOk1(25,2) =" + floorOk1(25 ,2) + " == BAD(25 ,2)=" + floorBAD(25 ,2) + " Ok=" + (floorBAD(25 ,2)==floorOk1(25 ,2)) ) ;
System.out.println(" floorOk1(25,2) =" + floorOk1(25 ,2) + " == ULP(25 ,2)=" + floorULP(25 ,2) + " Ok=" + (floorULP(25 ,2)==floorOk1(25 ,2)) ) ;
System.out.println(" floorOk1(125,3) =" + floorOk1(125,3) + " != BAD(125 ,3)=" + floorBAD(125,3) + " Ok=" + (floorBAD(125,3)==floorOk1(125,3)) ) ;
System.out.println(" floorOk1(125,3) =" + floorOk1(125,3) + " == ULP(125 ,3)=" + floorULP(125,3) + " Ok=" + (floorULP(125,3)==floorOk1(125,3)) ) ;
System.out.println(" floorOk1(24389,3) =" + floorOk1(24389,3) + " != ULP(24389,3)=" + floorULP(24389,3) + " Ok=" + (floorULP(24389,3)==floorOk1(24389,3)) ) ; ;
System.out.println(" floorOk1(24389,3) =" + floorOk1(24389,3) + " != BAD(24389,3)=" + floorBAD(24389,3) + " Ok=" + (floorBAD(24389,3)==floorOk1(24389,3)) ) ; ;
}
/*
Tests result are:
floorOk1(25,2) =5 == BAD(25 ,2)=5 Ok=true
floorOk1(25,2) =5 == ULP(25 ,2)=5 Ok=true
floorOk1(125,3) =5 != BAD(125 ,3)=4 Ok=false
floorOk1(125,3) =5 == ULP(125 ,3)=5 Ok=true
floorOk1(24389,3) =29 != ULP(24389,3)=28 Ok=false
floorOk1(24389,3) =29 != BAD(24389,3)=28 Ok=false
*/
I mainly tried BigDecimal as an alternative found one solution:
public static long floorDEC( final long in , final int exp) {
return new BigDecimal(Math.pow(in,1.0/exp),MathContext.DECIMAL32).longValue();
}
I’m programming an app for point cloud detection with ARCore in Java.
I want to get the 3D coordinates of all points, which are rendered in the frames. Currently I get only a very few of these points, if I use the following code.
pointCloud = frame.acquirePointCloud();
if (list1 != null) {
String x = String.valueOf(df.format(pointCloud.getPoints().get(0))).replace(",", ".");
String y = String.valueOf(df.format(pointCloud.getPoints().get(1))).replace(",", ".");
String z = String.valueOf(df.format(pointCloud.getPoints().get(2))).replace(",", ".");
String c = String.valueOf(df.format(pointCloud.getPoints().get(3))).replace(",", ".");
list1.add(x + ", " + y + ", " + z + ", " + c + "\n");
}
I only found one other implementation. I tried this one, but I got a lot of confusing points, which drifted away.
pointCloud = frame.acquirePointCloud();
if (list6 != null) {
FloatBuffer fb = pointCloud.getPoints();
while (fb.hasRemaining()) {
float x = Float.parseFloat(df.format(fb.get()).replace(",", "."));
float y = Float.parseFloat(df.format(fb.get()).replace(",", "."));
float z = Float.parseFloat(df.format(fb.get()).replace(",", "."));
float c = Float.parseFloat(df.format(fb.get()).replace(",", "."));
list6.add(x + ", " + y + ", " + z + ", " + c + "\n");
}
}
I´m not sure that it is a problem in the code.
Does sombody have a better idea to get all points or can tell me what I’m doing wrong?
I would be glad about some help. Thanks.
I have used this code for acquiring Point Cloud data. You can check the length of Point Cloud Ids array to know the exact number of Point Clouds.
Using the below code you can map the Point Data to exact ID and be sure if all the data is available.
PointCloud pointCloud = frame.acquirePointCloud();
IntBuffer pointCloudIds = pointCloud.getIds();
int[] pointCloudIdsArray = new int[pointCloudIds.limit()];
pointCloudIds.get(pointCloudIdsArray);
Log.i(TAG, "onDrawFrame: Point Cloud Id Array Length " +
pointCloudIdsArray.length);
FloatBuffer pointCloudData = pointCloud.getPoints();
float[] pointCloudArray = new float[pointCloudData.limit()];
pointCloudData.get(pointCloudArray);
Log.i(TAG, "onDrawFrame: Point Cloud Data Array Length " + pointCloudArray.length);
long PointCloudTimeStamp = pointCloud.getTimestamp();
if (pointCloudIdsArray.length > 0) {
for (int i = 0; i < pointCloudIdsArray.length; i++) {
Log.i("TAG",Long.toString(PointCloudTimeStamp) + ";" +
Integer.toString(pointCloudIdsArray[i]) + ";" +
Float.toString(pointCloudArray[i * 4]) + ";" +
Float.toString(pointCloudArray[i * 4 + 1]) + ";" +
Float.toString(pointCloudArray[i * 4 + 2]) + ";" +
Float.toString(pointCloudArray[i * 4 + 3]) + "\n");
}
}
Three parameters are passed into this function to output a depreciation table. The first loop calculates the sum of the years, while the second loop will calculate depreciation for each year during the useful life of the item entered by the user. I'm having trouble coding the second loop so that it outputs the correct values under the headers of the above System.out.println statement. As is the loop won't run since it's the same loop as the first one.
public class Table
{
public void makeDepreciationTable( int useful_Life, double acquisition_Value, double salvage_Value )
{
int sum = 0;
int year = 1;
double accumulatedDepreciation;
// make a loop to calculate the sum
while ( year <= useful_Life )
{
sum += year;
year++;
}
System.out.println("The sum is " +sum );
// write the header of the table
System.out.println(" Acquisition Value" + "" + " Salvage Value" + "" + " Useful Life" + "" + " Annual Depreciation" + "" + " Accumulated Depreciation" + "" + " Book Value" + "" +" Fraction");
// make a loop
while ( year <= useful_Life )
{
// calculate fraction
double fraction = (double)year/sum;
// calculate annualDepreciation
double annualDepreciation = (acquisition_Value - salvage_Value) * fraction ;
// calc accumulatedDepreciation
accumulatedDepreciation += annualDepreciation;
// calc bookValue
double bookValue = acquisition_Value - accumulatedDepreciation;
// write one line of table
System.out.println(acquisition_Value + "" + salvage_Value + "" + useful_Life + "" + annualDepreciation + "" + accumulatedDepreciation + "" + bookValue + "" + fraction);
year++;
}
}
}
Because year has become equal to the useful_Life during the end of the first while loop, you need to reset the year value to 1 again just before the 2nd while loop as shown below:
year = 1;//reset year before 2nd while
while ( year <= useful_Life ) {
//add your code
}
Add year =1 before second loop .
Because of first loop year value is incremented . But did not set it back to one before second loop
When I run my program instead of receiving the string from the toString method in my RightTriangle class that I should get normally I instead receive the memory location of the string when I print the object from my driver. How do I fix this problem?
this is the RightTriangleDriver class
import java.util.*;
public class RightTriangleDriver
{
public static void main ( String [] args )
{
Scanner reader = new Scanner ( System.in );
System.out.println ( "Enter the length of the first leg " );
double leg1 = reader.nextDouble ();
System.out.println ( "Enter the length of the second leg " );
double leg2 = reader.nextDouble ();
RightTriangle f1 = new RightTriangle ( leg1, leg2 );
System.out.println ( f1 );
}
}
this is the RightTriangle class.
public class RightTriangle
{
private double leg1;
private double leg2;
private double hyp;
public RightTriangle (double one , double two)
{
leg1 = one;
leg2 = two;
hyp = Math.sqrt ( Math.pow ( leg1 , 2 ) + Math.pow (leg2 , 2 ) );
}
public double perimiter ()
{
double perimiter = 0;
perimiter = leg1 + leg2 + hyp;
return perimiter;
}
public double area ()
{
double area = 0;
area = ( leg1*leg2 )/2;
return area;
}
public String toSting ()
{
String str;
str = "Leg 1 is " + leg1 + " units long. Leg 2 is " + leg2 + " units long." + "\n" + "the Hypotenuse is " + hyp +
" units long." + "\n" + " The perimiter is " + perimiter () + " units and the area is " + area () +
" units squared.";
return str;
}
}
The method name is toString. You have not written correctly.
Whenever you override method of super class use #Override annotation (toString is method of Object class which every class inherit by default). This will help you to know if you are really overriding the method.
Here's my source code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println( "Integer.MAX_VALUE = " + Integer.MAX_VALUE );
System.out.println( "Integer.MAX_VALUE + 1 = " + (Integer.MAX_VALUE + 1) );
System.out.println( "Integer.MAX_VALUE * 2 = " + ( Integer.MAX_VALUE * 2) );
System.out.println( "Integer.MAX_VALUE * 5 = " + ( Integer.MAX_VALUE * 5) );
System.out.println( "Integer.MAX_VALUE * 10 = " + ( Integer.MAX_VALUE * 10) );
System.out.println( "Integer.MAX_VALUE = " + Integer.MAX_VALUE );
System.out.println( "Integer.MIN_VALUE - 1 = " + (Integer.MIN_VALUE - 1) );
System.out.println( "Integer.MIN_VALUE * 2 = " + ( Integer.MIN_VALUE * 2) );
System.out.println( "Integer.MIN_VALUE * 5 = " + ( Integer.MIN_VALUE * 5) );
System.out.println( "Integer.MIN_VALUE * 10 = " + ( Integer.MIN_VALUE * 10) );
//Part 2
System.out.println( "Integer.MAX_VALUE + 1.0 = " + (Integer.MAX_VALUE + 1.0) );
System.out.println( "Integer.MAX_VALUE * 2.0 = " + ( Integer.MAX_VALUE * 2.0) );
System.out.println( "Integer.MAX_VALUE * 5.0 = " + ( Integer.MAX_VALUE * 5.0) );
System.out.println( "Integer.MAX_VALUE * 10.0 = " + ( Integer.MAX_VALUE * 10.0) );
System.out.println( "Integer.MIN_VALUE - 1.0 = " + (Integer.MIN_VALUE - 1.0) );
System.out.println( "Integer.MIN_VALUE * 2.0 = " + ( Integer.MIN_VALUE * 2.0) );
System.out.println( "Integer.MIN_VALUE * 5.0 = " + ( Integer.MIN_VALUE * 5.0) );
System.out.println( "Integer.MIN_VALUE * 10.0 = " + ( Integer.MIN_VALUE * 10.0) );
//Part 3
int a, b;
a = 1;
b = 2;
System.out.println( "The ints a, b are " + a + ", " + b );
System.out.println( "a + b is " + a + b );
System.out.println( "a - b is " + a - b );
System.out.println( "a * b is " + a * b );
System.out.println( "a / b is " + a / b );
//Part 4
double aD, bD;
aD = 1.0;
bD = 2.0;
System.out.println( "The doubles aD, bD are " + aD + ", " + bD );
System.out.println( "aD + bD is " + aD + bD );
System.out.println( "aD - bD is " + aD - bD );
System.out.println( "aD * bD is " + aD * bD );
System.out.println( "aD / bD is " + aD / bD );
}
}
Here's my error:
Compilation error time: 0.1 memory: 320512 signal:0
Main.java:37: error: bad operand types for binary operator '-'
System.out.println( "a - b is " + a - b );
^
first type: String
second type: int
Main.java:46: error: bad operand types for binary operator '-'
System.out.println( "aD - bD is " +aD - bD );
^
first type: String
second type: double
2 errors
I am new to java and I am still figuring out arithmetic. I thought I was doing fine but I don't understand what's gone wrong. It is most probably a real rookie mistake but could you tell me what I did wrong?
"a - b is " + (a) - (b)`
means:
("a - b is " + (a)) - (b)
. The left part (("a - b is " + (a))) is a string, and you can't subtract from a string.
You need to use parentheses:
"a - b is " + (a - b)
The + and - considering as string operations where the expression being evaluation from left to right. To avoid the situation mark the subtraction comes under high precedence which happens when you surround with ()
System.out.println( "a - b is " + (a - b) );
Now because of the precedence the expression inside parenthesis (a-b) evaluates first and then the whole expression from left to right.