Efficient Method To Round Double On 0.05
May 27, 2015
I've a question on rounding doubles on 0.05 As far as i know there is no class method that is able to do this. To achieve this rounding i have the following code:
Example:
164.67 -> 164.70
173.33 -> 173.35
0.02 -> 0.05
double calculatedQuantity = 0.0;
// Formula to calculate the average working hours in a month
int totalWeeksInYear = 52;
int totalMonthsInYear = 12;
[Code].....
View Replies
ADVERTISEMENT
Mar 28, 2015
I have a double/big decimal value with 2 decimal places....What I want is that if decimal value is .01 - .49, it should round up to 0.50.
Then if decimal value is between 0.51 - 0.98, it will round up to 0.99.
Is there a function in double that can do this? How can I do this?
View Replies
View Related
Jul 11, 2014
I'm having an issue with figuring out how to structure and call a method using a string prompt. My prof has given us specific methods to create, and the one I'm having issues with is blic static double getOperand(String prompt). What I have currently is this:
public static double getOperand(String operand1, String operand2){
Scanner input = new Scanner(System.in);
System.out.println("What is the first operand?");
operand1 = input.toString();
System.out.println("What is the second operand?");
operand2 = input.toString();
return operand1, operand2;
}
which is obviously not working. When I asked my prof he sent me this, "The String paramater in the getOperand() method is intended to have the prompt value that is passed to it. Within the method, the prompt is then displayed, what the prompt is asking for is input, and then returned to the method call.For example, if the method is public double getOperand(String prompt) within the method you would display the prompt, as in System. out. println(prompt), use the Scanner class to create an object, perhaps called input, and then use the input object to get the value asked for by the prompt. Once you have that value, which in this example would be a double, then return the value to the method that called it.
The call could be something like this: double value = getOperand("What is the first operand?");". But that only works a little. Clearly I'm missing something and no matter what I google, or how many times I read the chapter, I am just not getting this. Everything else up to this point has been easy. and I can create regular methods just fine, the menu method works, but I can't figure out this string method and all the rest build off this method.
View Replies
View Related
May 13, 2014
I am really struggling to make the Split String method work with another method which is double hours.
HtmlElement span = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span.setValue("Drive Time: ");
td.addNestedElement(span);
HtmlElement span2 = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span2.addAttribute("class", "drive_time");
String time=String.valueOf(showSet.getTransferHours());
/*Split the Strings into Hours and Minutes*/
[code]...
View Replies
View Related
Jul 9, 2014
What's that diameter? Create a new method for the circle class called diameter. Add this method to the circle class described on page 15-1. It should return a double that is the diameter of the circle. No parameters are passed to this method.
In a tester class, test the performance of your new diameter method as follows:
(Your project should have two classes, Tester and Circle.)
here is what i have so far:
public class Circle
{
public Circle (double r)
{
radius = r;
}
public double area()
{
double a = Math.PI * radius * radius;
[Code] ....
View Replies
View Related
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
Sep 27, 2014
The first problem is that the numbers don't round in IntelliJ.
Example: ('s in Dutch)
Geef de vorige kilometerstand: 125
Geef de huidige kilometerstand: 900
Geef het aantal getankte liters: 50
Verbruik voor 775km: 6.451612903225806/100km
That 6.4516..... is the problem, how can I make it 6.45/100km?
View Replies
View Related
Jan 30, 2014
my project flow is html,css,js<--->ajax<-->jsp<--->jdbc(.java)<--->mysql. In that jdbc i am returning the records from the database and fill into the resultset. From the resultset i put all records in arraylist and passed to jsp then i am displaying data in the screen.Whether i can use arraylist, hashmap or treetable?
View Replies
View Related
Jan 31, 2015
if one address point on another address. so set and get methods will be less efficient then an array, but add or remove will be more efficient by a linked list ? a linked list also inherit from queue so if i add an elemnt via "addFirst" function . where its adding the element ? to the right most or left most ?
if i have for example :
here [ ] --> [ ] --> [ ] --> [ ] -->[ ] or here
linked list its FIFO so the head will be the right most ?
Edit : its confused me a little bit but i understood now .so it will be at the left most. its actually ordered . not like the stack which is LIFO.
View Replies
View Related
Jan 11, 2015
My program is attempting to:
1. ask user to input a decimal
2. output 1 - confirms the input
3. output 2 rounds the input value to the nearest integer.
Here is what I have so far:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class IT145Exercise13 {
static Scanner console = new Scanner(System.in);
[Code] .....
Results with input of 5.25:
run:
Enter a decimal:
5.25
You entered 5.25
The integer value is 5 YES however,
BUILD SUCCESSFUL (total time: 6 second)
However, if the input is 5.95 here are the run results:
run:
Enter a decimal:
5.95
You entered 5.95
The integer value is 5 Here I expect 6
BUILD SUCCESSFUL (total time: 6 second
Not sure how to ask the output of the variable integerOut to be rounded to the nearest integer?
View Replies
View Related
Dec 2, 2014
for standard deviation my output is not rounded to the 10's place, how can I make it round to the 10's place. Here is my code.
import java.util.*;
public class chapter7 {
public static final int Max_Number_Scores = 100;
public static int fillArray(double[] scores) {
System.out.print("You entered ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
[code]....
View Replies
View Related
Jan 23, 2014
My GCD testing:
Java Code:
public static long gcd(long a, long b) {
if(b == 0) {
return a;
} else {
return gcd(b, a % b);
}
} mh_sh_highlight_all('java');
Is there a more efficient way? Also, what can I do to speed this forloop up?
Java Code:
for(long i = 1; i < 750000; i++) { dothis; } mh_sh_highlight_all('java');
View Replies
View Related
May 20, 2014
I'm currently working on a loan calculator... The only problem I'm having is occasional values that have very long decimals... How do I round the decimals?
View Replies
View Related
Oct 22, 2014
I'm looking for a way to gather data from a site page. all data is shown in the same page... I am trying to get the content and parse it is a bit crazy as data seems to be not organized. Itried to get it as a document but still looks crazy.
As all data is shown very clearly in the page (I would like every row to be an object) I'm sure there is some way to collect this data easily. (the data is from this page: [URL] ....)
I'll attach a snapshot and the content I got from the website.
in-play.jpgall_in-play_page.txtin-play.jpgall_in-play_page.txt
View Replies
View Related
Aug 20, 2014
I am kind of new to Java. I have a query.. look at it. Have an xml like
<ProductList>
<Product Quality='Good' color='Blue'>
<Details ItemID='1001'/>
</Product>
...
..
</ProductList>
The <Product> tags can run into hundreds or thousands. Quality can have values like Good, Bad, Damaged. Color can also have various values (Blue, Red ..)
ItemID can repeat or can be different. So I want group by ItemID->Color->Good count or BadCount and TotalCount as below.
I want to get a sum like
<ProductList>
<ProductType ItemID="1001">
<Product_Detail CountGood="1" CountBad="2" CountTotal="3" Type="Blue"/>
</ProductType>
...
...
</ProductList>
I think using Hashmaps (Hashmap1 for ItemID, Hashmap2 for color, Hashmap3 for quality) may do the work. But I have to parse the xml number of ItemID's multiply-by number of colors multiply by various colors times for this.
Any better algorithm is there in performance perspective using Java.
View Replies
View Related
Oct 18, 2014
This isn't solely related to java.awt.Color, it's more of a question on how to store methods or redirections to use methods on any value passed in. To explain my question I'll describe a scenario:
ClassA is an instanstiatable class
ClassA has an instance of ClassB called cB
ClassA has methods of affecting cB
ClassD is also an instanstiatable class
ClassD has an Array[x][y] of instances of ClassA
I want to create a ClassE which I would initiate it with references to ClassA's methods or their actual methods and then I can use ClassE to easily transform ClassD's instances of ClassA by the same methods.
What I'm actually doing:
I made a class called GameColor, it has methods like changeHue, setRed, setHue, changeBrightness, setSaturation etc, these methods affect an instance of java.awt.Color in the GameColor class instances, so like they can change the hue or set it with changeHue or setHue etc...
Now I want to do something like perform a single method on a whole BufferedImage, on each of it's pixels RGB. So, I'm thinking of creating a GameColorEffects class which I store references or actual methods of GameColor like changeHue, and then I use GameColorEffects somehow to change a whole BufferedImage more efficiently, what's the most efficient way I can store methods or their reference of GameColor and then later apply them on each pixel of a BufferedImage using GameColorEffects. So all I do is initiate GameColorEffects with methods or reference from GameColor, and then I can apply it to easily transform each pixel.
View Replies
View Related
May 20, 2014
I'm doing a bit of styling in TableView. The result is quite nice but I can't make the left top corner round. This is the actual result:
[URL] ....
And this is the css :
.table-view {
-fx-background-color: transparent;
}
/*HEADER */
.table-view .column-header{
[Code] .....
I would also like the top left corner was round.
View Replies
View Related
Feb 5, 2014
We are in the process of developing a e-commerce application. It is a web site for a book shop. It is a site very similar to Amazon.com where you can order books online. Front end is in Java Technology. There is however a concern about where to put the business logic.
I am suggesting to put all business logic in the Oracle Database, as stored procedures (i.e. packages). However, one of my colleague says that when you call a Oracle stored procedure from Java, it takes 2 round trips, one to validate the procedure and then to validate the input / ouput parameters of the procedure.
In a website application like what we are trying to build, is it sound advice to put all business logic in the DB? Or should it be in the middle tier (app. server) programmed in Java? Or should you spread in between the middle tier and DB? If so how?
View Replies
View Related
Feb 13, 2015
with arrays its binary search which finds a value in O(Logn) time but what about linked lists ? the most effiecient algorithm will be O(n) ? and i know that binary search cannot be implement on a linked list , therefore , the only way to search a linked list is a linear search ?
View Replies
View Related
Jan 7, 2014
I am trying to override the equals method for my class TeamMember I have two fields:
private Details details;
private double salary;
Code for override I get an error on salary saying cannot invoke equals (double) on the primitive type (double)
Is their something I am missing/coding wrong?
Java Code:
public boolean equals(Object obj) {
//test exceptional cases
//avoid potential NullPointerException/ClassCastException
if ((obj == null) || (this.getClass() != obj.getClass()))
return false;
TeamMember other = (TeamMember) obj; //cast to a TeamMember object
// compare fields details and salary
return this.details.equals(other.details)
&& this.salary.equals(other.salary);
} mh_sh_highlight_all('java');
View Replies
View Related
Apr 2, 2014
Here is my code and i want to convert number into 2 decimal but this code not give me result how to check my code.
import javax.swing.JOptionPane;
public class showtime{
public static void main(String[] args){
double total_mili=System.currentTimeMillis();
JOptionPane.showMessageDialog(null,total_mili, "Total milisecond",JOptionPane.INFORMATION_MESSAGE);
double seconds=total_mili/60;
double sec=(double)(seconds * 100) / 100.000;
JOptionPane.showMessageDialog(null,seconds, "Total Second",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,sec, "Total Second",JOptionPane.ERROR_MESSAGE);
}
}
View Replies
View Related
Jan 11, 2014
Java Code:
class Program {
public static void main (String[] args) {
long max = 200; //add up all primes up to 200
long ans = 0;
for(long x = 2; x < max; x++) {
if(isPrime(x)) {
ans += x;
[Code] ....
This code adds up all of the primes before a certain number (max). Unfortunately, it is much too slow when you get up to bigger numbers - how can I do this more efficiently.
View Replies
View Related
Nov 30, 2014
I'm having some trouble with a code I am writing for class. I had an 2 errors like this before this one and fixed it by changing int avgRe, avgMiles =0; to double. Now I am getting this error and am stuck on what I need to change. Here is the code:
import java.io.*;
import java.util.*;
import java.text.*;
public class Reimbursement_3_09 {
static Toolkit tools = new Toolkit();
public static void main (String [] args) throws Exception {
[Code] ....
This is my error:
[code=Java]
Reimbursement_3_09.java:33: error: incompatible types: possible lossy conversion from double to int
summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles);
^
Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
View Replies
View Related
Jun 17, 2014
how can i compare two double values is this correct
Java Code:
public boolean isEqualToD(double s_src, double s_compareTo)
{
boolean flag = false;
try
{
if(s_src == s_compareTo)
{
flag = true;
[code]...
View Replies
View Related
Apr 11, 2015
Okey so i want to make an array like this
km = miles
10 6.214
15 9.321
20 12.428
and so on its a converter between km and miles and i have no clue how to do this...
View Replies
View Related
Feb 14, 2014
Java Code:
double Compact=30.50;
double Full=40.50;
double discount=.07;
double FullDiscount= 37.67;
double CompactDiscount= 28.37; mh_sh_highlight_all('java');
My question is how do I configure this so that when / if I need to change the discount value, it automatically adjusts the % discount off of the Full or Compact?
All I can get to work is simply hardcoding it and assigning the discount value, i'm probably just messing up the syntax.
View Replies
View Related