I'm using eclipse. I'm going to get straight to the point and give all the info I can, if the values in the first code box are used, shouldn't these values be left after all in the second box is done:
remainder=23, arr[0]=100, div=23/10=2.3, whole=2, and decimal=3?
When I use this code, div comes out to be just (2.0).
Java Code:
int leng=10;
arr[0]=123; //int
arr[1]=100; //int mh_sh_highlight_all('java'); Java Code: if (arr[0]!=arr[1]){
int remainder=arr[0]-arr[1];
arr[0]=arr[0]-remainder;
double div=remainder/leng; //double div=Double.valueOf(remainder/leng);
int whole=(int) Math.floor(div);
int decimal=(int) ((div-whole)*leng); mh_sh_highlight_all('java');
I'm not sure were I'm going wrong in how div is being calculated, but I ultimately need div to be 2.3.
I've also used the second option commented out which still gives (2.0).
I am writing a simple learning program that does basic math. Everything is working as needed except for when it comes to division. I need to know if there is a way to format the users input to two decimal places and that when the program checks the users answer against the division, it gives it a yay or nay. Right now, it wants the answer out to 12 decimal places.
I am having trouble figuring out where to put %.2f in my code. I need it to get my answers for surfaceArea and Volume to be rounded to two decimal points. I have everything completed but wherever i put it, it seems to not work.
System.out.println("This program will compute the volume and surface area of a rectangular prism."); String output; double length; double width; double height; double volume = 0; double surfaceArea = 0; System.out.printf("%.2f", surfaceArea);
package FracCalc; import java.util.Scanner; public class FracCalc { public static void main(String[] args) { Scanner scan = new Scanner(System.in);
int n1 = 0; //n1 = first numerator user have to enter int d1 = 0; //d1 = first denominator user have to enter int n2 = 0; //n2 = second numerator user have to enter int d2 = 0; //d2 = second denominator user have to enter int numeratorAnswer = 0; //calculates numerator of an equation int denominatorAnswer = 0; //calculates denominator of an equation
[code]...
I mean its not that important but i figure it will be better if I could print out the answer both in fraction format and decimal format. while my answer prints in fraction just fine how do i show it in decimal?
System.out.println("");
System.out.print("And "); System.out.print("answer in Decimal is: ");
Add the following method to the BankAccount class: public String toString()Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.toString() should return:
Benson, $17.25
There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:
Benson, -$17.50
Here is my code:
public String toString() { String result = name + ", "; if (balance < 0) { result += "-"; } return result += "$" + Math.abs(balance); }
My code only works in case there are full two numbers for the cents part, not for the case when there's only one number. So I wonder how I can add an extra zero when needed.I can get only the decimal part and add a zero if it's less than 10, but I don't know how I can extract just the decimal part from the number. (The balance is just a double and it doesn't have any separate field for dollars and cents).
I do not know how to convert a reversed array to decimal.
The output should be:
How many digits to convert? (user will input) ex. 3 Please input digits: (user will input 3 digits) ex. 0 1 1 The binary digits are: 110 (<-reversed) The decimal value is: 6
Here is my code:
package numbersystemconversion; import java.util.Scanner; public class BinarytoDecimal { static { int digit=0; Scanner input = new Scanner(System.in);
The code is below and the problem I am having is the numbers after the decimal won't add together for the total I am teaching myself java through liang's 10th edition book and I took one of the sample programs further than what was shown. The one in the book only shows the tax amount I simply wanted to make it so that the purchase amount and the tax amount would add together
package javalearning; import java.util.Scanner; public class FindingTax { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter Purchase amount: ");
I'm trying to complete a code to convert decimal to octal. however i can't figure out how to print the output correctly. it should be.. Your integer number 160000 is 0470400 in octal. (160000 is the number you input). My code is...
Scanner input = new Scanner(System.in);
int value; int a; int b; int c; int d; int e; int f; int g; String result;
[Code] .....
Lastly, my output prints everything correctly, but the input number always is 0.
Ex error: input= 160000
Your integer number 0 is 0470400 in octal.
it should be.. Your integer number 160000 is 0470400 in octal.
I am using NetBeans IDE 8.0.2 to program Java code. I am a beginner in Java.
Instead of getting two decimal places after the point, I am getting three despite using the code format for two ("##.##"). Actually, this happens even if I remove the format code between the quotes. It is as if the program cannot see the format code. Why this happens ?
Here is the relevant program code:
private void convertButtonActionPerformed(java.awt.event.Action Event evt) { double inputNumber = 0; // sets the decimal format
designing a program which allows you to buy stuff from the jframe with 10x items each with different pricing, so once you click the image button the cost of that is displayed in the textfield, ive been trying to set the jtextfield to a decimal point but have not been able to so far,
jTotal.setText(Double.toString(dTotal)); DecimalFormat myFormatter = new DecimalFormat("#####.#"); String output = myFormatter.format(dTotal); a visual aspect of it.
I am having an issue with my program. It is supposed to convert from a binary number to a decimal number such as bin 101 = dec 5. my first and foremost issue is that when I use System.out.println(parseBin("101")); it returns 5 as it should. However when I change 101 to 1013 it returns 13??? Why is this happening and why are my exceptions not catching the issue?
//import java.util.Scanner; public class BinaryFormatException { public static void main(String[] args) throws BinFormatException{ System.out.println(parseBin("1013")); //Scanner input = new Scanner(System.in); //System.out.println("Please enter a binary number using 1's and 0's: "); //String binString = input.nextLine();
I need this program to print out the gpa down to 2 decimal places and I can't figure out how to do it. It keeps saying it can't find decimal format and I'm not sure how to define it.
Java Code:
import java.util.Scanner; public class GPACalculator { public static void main(String[] args) { double creditHours = 0; double gradePoints = 0;
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Sample3 { public static void main(String args[]){ double amount,iRate,monPay,totalPay; int years; String amountStr;
I'm trying to convert an octal number into a decimal number. I keep getting a big error that says java:63: error: class, interface, or enum expected. i need to use a public static int convert ( int octalNumber ) along with a public static void main ( String args[] ).
Here is my code. />
[public static void main ( String args[] ) { int foo; int valid = 0; Scanner sc = new Scanner (System.in); System.out.print ("Enter up to an 8-digit octal number and I'll convert it: "); foo = sc.nextInt();
I am a beginner in Java and I am currently learning about JOption pane.Using Strings I am accepting an input from the user and by using the Interer.ParseInt(variable) option I am able to multiply this two strings using the code below.
String Length; Length = JOptionPane.showInputDialog("Enter the Length"); String Breadth; Breadth = JOptionPane.showInputDialog("Enter the Breadth"); System.out.println(" Area is " + (Integer.parseInt(Breadth) * Integer.parseInt(Length))); System.exit(0);
How Do I make my code accept Decimal values. E.g. My Code should accept 10.02 as Length and 20.42 as Breadth and give the product a Decimal.
so I need my program to print in decimal format and I keep getting an error saying that it cant find symbol "decimalFormat". here's what I have so far.
import java.util.Scanner; public class GPACalculator { public static void main(String[] args) { int creditHours = 0; int gradePoints = 0;
I'm working on creating the Binary to Decimal program . hat is wrong with this part of my code. Why does it not take you into the loop.
import java.util.Scanner; public class question5 { public static void main(String[] args) { System.out.println("Enter a Binary number. "); // collect Scanner keyb = new Scanner(System.in); //
I am currently trying to solve a programming problem on this site, and the problem includes working on a 100 digits of decimal places of a certain irrational number. I tried using BigDecimal class but it displays, correctly me if I am wrong, just 20+ decimal digits. Is there other way to do this?
I am trying to convert the double grossPay to 2 decimal places but cannot get it to work ,I am unsure of the correct way of doing this but this is how far I can get
I am working on a program where i calculate a fee using a method. I know i have to import the decimal format utility of java. "import java.text.DecimalFormat;" then i have to create an object for the decimal format. With "DecimalFormat f = new DecimalFormat("0.00");" What i am wondering how do i apply it to my system.out.println statement. I know i have to use for instance f.format(calculateFee()); Is that the right syntax for displaying the results because i generate a syntax error that way.
I am trying to cast an integer into a double. it works but I need two decimal number after the dot and for example I can print 7.51 but how can I get it to print 7.50 and not 7.5
public int compareTo(Money o){ int d1 = (this.dollars * 100) + this.cents; int d2 = (o.dollars * 100) + o.cents; return d1 - d2; }
On my main method I have
public static void main(String[] args){ Money money1 = new Money(10, 49); Money money2 = new Money(2, 99); System.out.println("result: " + "$" + (double)money1.compareTo(money2)/100); }
It prints 7.5 but I want it to print 7.50, how do I do this .....