Convert feet to inches - java

I'm trying to make a program that converts inches to feet, and returns the number of feet and the number of leftover inches if any. I tried this:
public class Convertor
{
/**
* Fields
*/
private int inches;
private int feet;
private int yards;
private int leftoverInches;
/**
* Constructor for objects of class Convertor
*/
public Convertor()
{
inches=0;
feet=0;
yards=0;
leftoverInches=0;
}
/**
* Mutator method to convert inches to feet
*/
public void convertValuesInchtoFeet(int anyInches)
{
inches=anyInches;
feet=(anyInches * 0.083);
leftoverInches= inches%feet;
System.out.println(inches+" inches = " +feet+" feet.");
System.out.println("There are " +leftoverinches +" leftover inches");
}
Doesn't work.
Someone help me on this, please! Thank you.

int inches = 34;
int feet = inches / 12;
int leftover = inches % 12;
System.out.println(feet + " feet and " + leftover + " inches");

try this:
public void convertValuesInchtoFeet(int anyInches)
{
inches = anyInches;
feet = Math.floor(inches/12);
//if int than no need for the Math.floor()
leftoverInches = inches%12;
System.out.println(inches + " inches = " + feet + " feet.");
System.out.println("There are " + leftoverInches + " leftover inches");
}

The primary reason your code doesn't work is because you're doing
leftoverInches = inches%feet;
Suppose you gave it 13 inches. You would have feet = 1 (13 * 0.083 rounded down), and inches = 13 % 1 = 0. What you mean to do was
leftoverInches = inches%12;
With 13, 13%12 = 1, which is indeed the number of leftover inches.
A smaller but still important error is that you multiply by 0.083, which is NOT 1/12, and will give you serious inaccuracies. For example, if you enter 1,000,000 inches, you will get
1000000 * 0.083 = 83000 feet
But
1000000 / 12 = 83333 feet rounded down
So you would be 333 feet off.

Related

Why do I get number 320.04 printed to the console in the below code?

I read the below code a lot of times and I cannot figure out why I get the 320.04 on the console. Can someone kindly help me figure out?
package com.heshanshivantha;
public class Main {
public static void main(String[] args) {
calcFeetAndInchesToCentimeters(126);
}
public static double calcFeetAndInchesToCentimeters(double feet, double inches) {
if (feet >= 0 && inches >= 0 && inches <= 12) {
System.out.println(feet * 12 * 2.54 + inches * 2.54);
double centimeters = feet * 12 * 2.54 + inches * 2.54;
System.out.println(feet + " feet " + inches + " inches = " + centimeters + " cm");
return centimeters;
} return -1;
}
public static double calcFeetAndInchesToCentimeters(double inches) {
if (inches >= 0) {
int feet =(int) (inches / 12);
int remainingInches =(int) (inches % 12);
System.out.println(inches + " inches is equal to " + feet + " feet and " + remainingInches + " inches");
return calcFeetAndInchesToCentimeters(feet, remainingInches);
} return -1;
}
}
126.0 inches is equal to 10 feet and 6 inches
320.04
10.0 feet 6.0 inches = 320.04 cm
Process finished with exit code 0
That's because of the first line after the if condition.
Remove System.out.println(feet * 12 * 2.54 + inches * 2.54); and you are done.

why am i not able to get the return value of -1 when the condition is not met?

why am i not able to get the return value of -1 when the condition is not met?
package calcfeetandinchestocm;
public class Calcfeetandinchestocm {
public static void main(String[] args) {
calcFeetAndInchesToCentimeters(4, 12);
calcFeetAndInchesToCentimeters(-0.5);
}
public static int calcFeetAndInchesToCentimeters(double feet, double inches) {
if (feet >= 0) {
System.out.println("feet are " + feet + \
" and its equal centimeters are " + feet * 12);
}
if (inches >= 0 && inches <= 12) {
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
}
return -1;
}
public static int calcFeetAndInchesToCentimeters(double inches) {
if (inches >= 0) {
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
}
return -1;
}
}
I'll address your question first.
why am i not able to get the return value of -1 when the condition is not met?
You do not assign the result of the method call to a variable, so it's unclear how you recognize that you're not getting -1. In fact your methods as you have written them will always return -1 since that is the only return statement you have within the method body.
If you were to assign the result to a variable like this:
int centimeters = calcFeetAndInchesToCentimeters(4, 12);
System.out.println("4 feet 12 inches converted to centimeters is: "+centimeters);
The call to System.out.println above would output 4 feet 12 inches converted to centimeters is: -1
There are a few other problems with your code:
You are not correctly calculating the result of the conversion.
You have a second method with the same name as the first but with fewer parameters and you're repeating part of the logic in the first method.
To address #1 - one foot is 30.48cm so obviously this line is incorrect:
System.out.println("feet are " + feet + \
" and its equal centimeters are " + feet * 12);
Furthermore one inch is 2.54cm so your other calculation is also incorrect:
System.out.println("inches are " + inches + \
"and its equal centimeters are " + inches * 2.45);
I think it's probably obvious after what I said in the beginning, but you're also not returning the result of your calculation which is what I believe you intend to do. You need to convert feet to centimeters and inches to centimeters then return the sum of those two results.
To address #2 - your second method should probably not have the same name as it does not take a feet parameter at all.
Something like this would be more accurate:
public static int calcInchesToCentimeters(double inches)
Inside of this method you should not repeat the logic you have already written earlier in the code, instead just call your first method with 0 as the number of feet and pass the number of inches.
public static int calcInchesToCentimeters(double inches){
return calcFeetAndInchesToCentimeters(0, inches);
}
Lastly some further reading I would suggest for you:
Java Tutorial: Returning a Value from a Method
The DRY Principle
You have to set a variable equal to the function or do a system out print with the function as it's argument like:
System.out.print(calcFeetAndInchesToCentimeters(4, 12));
or
x = calcFeetAndInchesToCentimeters(4, 12);
System.out.print(x);
Here in your code, it dose not depend on condition so either your condition is true or false it returns always -1 so, you just need to grab return value from function. Like,
int result = calcFeetAndInchesToCentimeters(4, 12);
System.out.println("Result is " + result);
it surely print -1.

Sea Level Java Project

I am trying to develop a program that calculates the sea level all the way up to the year 2100. Based on research I have found that the sea level will be 2.5 feet(which is 30 inches) - 6.5 feet(which is 78 inches). My program asks the user to enter which year they want to calculate how many inches the sea level has risen but I want the information to be random numbers per year between .3488 and .9069 inches because those are the averages per year that will make the sea level 2.5 feet and 6.5 feet. So my question is how can i generate random numbers for each year the user inputs so I can calculate how much the sea level has risen and then output that in the number of gallons.
/takes the input from the user of all the way up to which year
/they want to know the number of inches/ gallons the sea level has risen
/using reiman summs and a chart
/
/
/*******************************************************************************/
import java.util.Random;
import java.util.Scanner;
public class CalcProject
{
public static void main(String[] args)
{
//variables for the program
int year;
int numOfYears;
double leastRise = .3488; //y = .3488x , x is the inches of rainfall
double highRise = .9069; //y= .9069x , x is the inches of rainfall
double seaLevel1;
double seaLevel2;
//Describing what the program does to the user
System.out.println("This program caluclates the number of gallons / inches" +
"the sea level will rise based on your input." + "\n");
//Asking for the users year they want to calculate
System.out.println("Please enter which year you wish to know how much the water level will have risen since 2014");
Scanner scan = new Scanner(System.in);
year = scan.nextInt();
if(year <= 2014 || year > 2100)
{
do
{
System.out.println("Please enter a valid year between 2015 and 2100");
year = scan.nextInt();
} while((year <= 2014 || year > 2100));
}
//puts the year into a number of years from 2014
numOfYears = year-2014;
System.out.println("The number of years between now and the year you chose is: " + numOfYears);
//uses the least inches of sealevel
seaLevel1 = numOfYears * leastRise;
System.out.println("The total sealevel in inches is " + seaLevel1 + " for " + year);
//uses the most inches of seaLevel
seaLevel2 = numOfYears * highRise;
System.out.println("The total sealevel in inches is " + seaLevel2 + " for " + year);
//calculating radnom numbers for each year
Here is a simple way of handling the weird random numbers.
int numOfYears;
double leastRise = .3488;
double highRise = .9069;
double seaLevel3 = 0;
int i = 0;
while (i < numOfYears) {
double rand = Math.random();
if (rand >= leastRise && rand <= highRise) {
// rand is some sea level value representing a year
seaLevel3 += rand;
i++;
}
}
System.out.println("The total sealevel in inches is " + seaLevel3 + " for " + year);

How do I calculate factorial and show working?

Hey I am really new to java and need a little help with this please.
I have some basic code that works fine it calculates the factor of a number lets say 5 and it gives the output answer of in this case "Factorial = 120.00"
That's great but I want it to give me the output like this "5 * 4 * 3 * 2 * 1 = 120.00" but I just can't figure out how to do it.
Thanks for any help
public static void main(String[] args)
{
Scanner kboard = new Scanner(System.in);
int nos1=0;
int total=1;
DecimalFormat df = new DecimalFormat("#.00");
System.out.print("Please enter number to factor ");
nos1 = kboard.nextInt();
for (int x=1;x<=nos1;x++)
{
total = total *x;
}
System.out.println("Factorial = "+df.format(total));
}
This will only get you part of the way there.
this code will print
1 * 2 * 3 * 4 * 5 * = 120
for (int x=1;x<=nos1;x++)
{
System.out.print(x + " * ");
total = total *x;
}
System.out.println(" = " + df.format(total));
I'll let you figure out a way to print it in the order you want and get rid of the last * at the end. there are a few ways.
String s = "";
for (int x = nos1; x >= 1; x--) {
total = total * x;
// print here it will work
if(!s.isEmpty())
s+="*";
s += x;
}
System.out.println(s + "=" + df.format(total));

Change Machine Math & Logic Errors

I've posted this program once before but realized I was overthinking it by adding loops and what not. I've paired it down a bit but still running into problems. The program is supposed to be a change machine. After the user inputs price, the program should round it up to the nearest dollar then output how much change will be dispensed and a count of which coins. The output is completely wrong at this point. I'm very new to programming and I'm at a loss.
package changemachine;
import java.util.Scanner;
import java.text.*;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Purchase Price: ");
double price = scan.nextDouble();
int newPrice = (int)(price*100);
int paid = (int)(newPrice+1);
int change = (int)(paid - newPrice);
int quarters = (int)(change/25);
int dimes = (int)((change%25)/10);
int nickels = (int)((change%25%10)/5);
int pennies = (int) (change%25%10%5);
System.out.println("Dispensing: " + quarters + " Quarters,"
+ dimes + "Dimes," + nickels + "Nickels,"
+ pennies + "Pennies.");
System.out.println("Program written by Ashley ");
}
}
(Once newPrice is an int, you can stop casting every line.) Instead of chaining % together, it would be more readable (and less error prone) to subtract off the values you've found:
change -= 25*quarters;
dimes = change / 10;
change -= 10*dimes;
nickels = change / 5;
change -= 5*nickels;
pennies = change;
I think it would help you to understand if you would go through the code by hand and think about what price, newprice, paid, and change are.
newprice is the price round down to the lower dollar.
paid is the cost of the item.
change is the amount you paid minus the cost converted into an integer number of pennies.
package changemachine;
import java.util.Scanner;
import java.text.*;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Purchase Price: ");
double price = scan.nextDouble();
int newPrice = (int)(price);
int paid = (int)(newPrice+1);
int change = (int)((paid - price) * 100);
int quarters = (int)(change/25);
int dimes = (int)((change%25)/10);
int nickels = (int)((change%25%10)/5);
int pennies = (int) (change%25%10%5);
System.out.println("Dispensing: " + quarters + " Quarters,"
+ dimes + "Dimes," + nickels + "Nickels,"
+ pennies + "Pennies.");
System.out.println("Program written by Ashley ");
}
}
If instruction int paid= (int)(newPrice+1) ; is supposed to be rounding to next dollar, then it should be: int paid= ( newPrice + 99 ) / 100 * 100 ; You don't need to convert to (int) when both operands are already ints. Makes your program slightly illegible. Later, after obtaining the number of quarters by quarters= change / 25 ;(that's correct in your program), you can reduce the amount fromchangewithchange-= quarters * 25 ;`.
This makes calculating dimes exactly the same as quarters, just that using 10 instead of 25. Don't forget reducing the dimes from the pending change again with change-= dimes * 10 ;. You can repeat the process with nickels and the remaining change will be pennies.
If you have any doubt, use a debugger or output each intermediate result with System.out. You can always delete them later once you understand your program's behavior.
This is how I made Java choose what coins I must pay with.
int temp = m;
int quarterCoin = 25;
int x = m/quarterCoin;
m=m-x*quarterCoin;
int dimeCoin = 10;
int z = m/dimeCoin;
m=m-z*dimeCoin;
int nickelCoin = 5;
int y = m/nickelCoin;
m=m-y*nickelCoin;
int pennyCoin = 1;
int w = m/pennyCoin;
m=m-w*pennyCoin;
Instead of giving you the answer/solution to your homework, I am going to help you figure out how to figure it out. :)
In order to adequately debug your software and troubleshoot what's going on, you need to know what your variables are doing. There are two methods:
Attach a debugger - Most IDEs will come with a debugger that will help you accomplish this.
Print out your variables to the console. This is my preferred method. Me and debuggers never have gotten along well together. :)
So, here is what I would do if I were trying to figure your program out:
import java.util.Scanner;
public class Change {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// System.out.println("Enter Purchase Price: ");
double price = 5.65d;//scan.nextDouble();
int newPrice = (int) (price * 100);
System.out.println("newPrice: " + newPrice);
int paid = (int) (newPrice + 1);
System.out.println("paid: " + paid);
int change = (int) (paid - newPrice);
System.out.println("change: " + change);
int quarters = (int) (change / 25);
int dimes = (int) ((change % 25) / 10);
int nickels = (int) ((change % 25 % 10) / 5);
int pennies = (int) (change % 25 % 10 % 5);
System.out.println("Dispensing: " + quarters + " Quarters,"
+ dimes + "Dimes," + nickels + "Nickels,"
+ pennies + "Pennies.");
System.out.println("Program written by Ashley ");
}
}
(Note: Instead of utilizing the scanner, I just manually entered "5.65" into the price variable just to save time)
Which produces the output:
newPrice: 565
paid: 566
change: 1
Dispensing: 0 Quarters,0Dimes,0Nickels,1Pennies.
Program written by Ashley
So, now you can see what your program is doing wrong. Can you spot it?

Categories

Resources