Please help me with this netbeans assignment code. I have been working on it for a few hours, and I don’t understand it.
Thanks!
I would avoid using raw user input for now, and just focus on creating a reusable method that does your calculation internally. There is no reason to cast to a float, because all the values should be floating-point values.
The calc method handles the calculation that you have copy-pasted three times.
public class CalculateHalfLife {
public static double calc(double amount, double halfLife, double hours) {
return amount / (Math.pow(2, (hours / halfLife)));
}
public static void main(String[] args) {
double amount = 100; // mg of caffeine
double halfLife = 6; // hours
double[] allHours = { 6, 12, 24 };
for (double hours : allHours) {
double output = calc(amount, halfLife, hours);
System.out.printf("After %.0f hours: %.2f mg\n", hours, output);
}
}
}
If you want to to support user input, you should prompt the user to enter input:
import java.util.Scanner;
public class CalculateHalfLife {
private static double amount = 100; // mg of caffeine
private static double halfLife = 6; // hours
private static double[] allHours = { 6, 12, 24 };
public static double calc(double amount, double halfLife, double hours) {
return amount / (Math.pow(2, (hours / halfLife)));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an amount: ");
float amount = sc.nextFloat();
System.out.println();
for (double hours : allHours) {
double output = calc(amount, halfLife, hours);
System.out.printf("After %.0f hours: %.2f mg\n", hours, output);
}
sc.close();
}
}
Related
In this HackerRank challenge, I need to find the total meal cost by adding tip_percent which is 20% of the meal_cost and tax_percent which is 8% of the meal_cost and the meal_cost being $12. So the output must be a round number of 15 but my output comes out as $14.
It does seem to work properly with custom values like $12.50 for meal_cost which later totaled comes out as a rounded value of $16. What am I doing wrong here?
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent) {
tip_percent = (int)(meal_cost * tip_percent)/100;
tax_percent = (int)(meal_cost * tax_percent)/100;
return meal_cost + tip_percent + tax_percent;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
double meal_cost = scanner.nextDouble();
int tip_percent = scanner.nextInt();
int tax_percent = scanner.nextInt();
//changed solve to mealTotal
double mealTotal = findMealTotal(meal_cost, tip_percent, tax_percent);
System.out.println(Math.round(mealTotal));
scanner.close();
}
You are using integers. Integers are rounded, so you loose precision over the next calculation. Try using doubles and cast to int at the end.
static void Main(string[] args)
{
double cost = findMealTotal(12, 20, 8);
Console.WriteLine(cost.ToString());
}
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent)
{
double tip = meal_cost * tip_percent / 100;
double tax = meal_cost * tax_percent / 100;
return meal_cost + tip + tax;
}
And don't reuse parameters inside your function. It is bad practice.
For whatever reason, my variable 'hours' is only existing as 0. When I enter 96 as distance, 1 as hours, and 43 as minutes, my answer is 133.9 mph. It SHOULD be 55.9223. I apologize for asking what I'm sure is an obvious question, but I'm very early in learning java.
import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;
public class mph
{
private double distance, hours, minutes;
private double mph;
public mph()
{
setNums(0,0,0);
mph=0.0;
}
public mph(double dist, double hrs, double mins)
{
distance=dist;
hours=hours;
minutes=mins;
}
public void setNums(double dist, double hrs, double mins)
{
dist=distance;
hrs=hours;
mins=minutes;
}
public void calcMPH()
{
hours=minutes/60; //hours is not being input, it counts as zero regardless of what's entered
mph=distance/hours;
}
public void print()
{
System.out.print((int)distance+" miles in "+(int)hours+" hours and "+(int)minutes+" minutes = "+mph+" mph.\n");
}
}
-------------------separate file----------------
import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;
public class mphRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(in);
out.print("Enter the distance :: ");
double dist = keyboard.nextInt();
out.print("Enter the hours :: ");
double hrs = keyboard.nextInt();
out.print("Enter the minutes :: ");
double mins = keyboard.nextInt();
mph test = new mph(dist, hrs, mins);
test.calcMPH();
test.print();
}
}
I apologize if I messed up the proper formatting somehow, this is my first post on the site.
Edit: Oh my god I'm a moron.
You are assigning your variable hours to itself instead of to the parameter hrs. Change
hours=hours;
to
hours=hrs;
This is what an idiomatic version of your attempt would look like:
Q33636764.java
import java.util.Scanner;
public class Q33636764
{
private static int getData(final Scanner scanner, final String message)
{
System.out.print(message);
if (scanner.hasNextInt()) { return scanner.nextInt(); }
else
{
System.out.println("Please enter an Integer!");
return getData(scanner, message);
}
}
public static void main(final String[] args)
{
final Scanner keyboard = new Scanner(System.in);
final int dist = getData(keyboard, "Enter the distance : ");
final int hrs = getData(keyboard, "Enter the hours : ");
final int mins = getData(keyboard, "Enter the minutes : ");
final Mph mph = new Mph(dist, hrs, mins);
System.out.format("%d miles in %d hours and %d minutes %.2f mph.", dist, hrs, mins, mph.calculate());
System.out.println();
}
public static class Mph
{
private final double distance;
private final double hours;
private final double minutes;
public Mph(final double distance, final double hours, final double minutes)
{
this.distance = distance;
this.hours = hours;
this.minutes = minutes;
}
public double calculate()
{
return this.distance / this.hours + this.minutes / 60;
}
}
}
Outputs:
Enter the distance : 100
Enter the hours : 1
Enter the minutes : 0
100 miles in 1 hours and 0 minutes 100.00 mph.
Here is my program. Just a little one for end of chapter exercises. Problem is that when I run it, it only works when I have %f in my System.printf when in my mind it should be a %d. All of my variables are double and Math.ceil returns a double so I am super confused. Could someone clarify please?
import java.util.Scanner;
public class Excersise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double enteredHours = 0;
double amountDue = 0;
System.out.printf("Please enter the number of hours parked.");
enteredHours = input.nextDouble();
amountDue = calculateCharges(enteredHours);
System.out.printf("The amount due is %f\n", amountDue); // HERE the line
// that when I
// changed it to
// %f it works,
// but again all
// variables are
// double and
// Math.ceil
// returns a
// double.
}
public static double calculateCharges(double hours) {
double roundedHours = 0;
roundedHours = Math.ceil(hours);
double charges = 2.00;
if (roundedHours > 3.0)
charges = charges + .5 * roundedHours;
if (roundedHours >= 24.0)
charges = 10.00;
return charges;
}
}
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html
%d is to format an 'integer' %f is for formatting floats/doubles. I think you're mistaking '%d' to stand for double
%f is the format specifier for floating point variables whereas %d is used for integer variables.
%f is used for formatting floats/double and %d is used for decimal and In java %d is used for display the result for example :-
public class Exercise
{
public static void main(String[] args)
{
long number = 30208;
System.out.printf("Number: %d", + number);
}
}
here we use %d for display the value of long variable
package presentvalue;
import java.util.Scanner;
public class PresentValue {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
double P; // present value
double F; // future value
double r; // annual interest rate
double n; // number of years
P = presentValue();
F = futureValue();
r = annualInterest();
n = years();
System.exit(0);
}
public static double presentValue()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the presnt value of you savings account?");
return keyboard.nextDouble();
}
public static double futureValue()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("How much do you want in your account in the future?");
return keyboard.nextDouble();
}
public static double annualInterest()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is youe bank's interest rate?");
return keyboard.nextDouble();
}
public static double years()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many years will the money in the bank?");
return keyboard.nextDouble();
}
public static double presentValue(double F, double r)
{
return F/(1+(r * r));
}
public static void show(double presentValue)
{
System.out.println(presentValue);
}
}
The question says write a method presentValue that preforms this calculation. The method should accept the future value, annual interest rate, and number of years as arguments. It should return the present value, which is the amount that you need to deposit today. Demonstrate the method in a program that lets the user experiment with different values for the formula's terms.
Here is the formula P = F/(1+r)^2
You will have to use Math.pow:
public static double presentValue(double future, double intrest, double years)
{
return future / Math.pow(1.0 + intrest, years);
}
Note that I added the amount of years. You stated two years, but I think you really want to have a parameter to specify the number of years, because, otherwise you wouldn't ask the user for an amount of years, right?
I am having difficulty passing the hours and hourlyWage arguments to the constructor in the Paycheck class. The issue is as follows:
symbol: variable hours
location : class Paycheck
It repeats for every instances of hours or hourly wage in public class Paycheck.
The code is as follows
import java.util.Scanner;
public class PayDayCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Hourly wage: ");
double hourlyWage = in.nextDouble();
System.out.println("Hours worked: ");
double hours = in.nextDouble();
Paycheck paycheck = new Paycheck(hourlyWage, hours);
System.out.println("Pay: " + paycheck.getPay());
}
}
public class Paycheck {
private double pay = 0;
private double overtime = 0;
private double overtimePay = 0;
/*double hours;
double hourlyWage; */
Paycheck(double hourlyWage, double hours) {
setPay(0);
}
public void setPay(double newPay) {
if (hours > 40) {
overtime = hours % 40;
hours = hours - overtime;
}
overtimePay = hourlyWage * 1.5;
pay = (hours * pay) + (overtime * overtimePay);
}
public double getPay() {
return pay;
}
}
You have commented out member variable hours:
/*double hours;
double hourlyWage; */
but still try to refer to it, e.g.:
if (hours > 40) {
overtime = hours%40;
hours = hours - overtime;
}
If you need this variable - uncomment it.
Your setPay method is referring to hours and hourlyWage, which are parameters passed in to the constructor, making them local only to the constructor. They are not available to all methods in the class. Those need to be uncommented at the class level if you want all methods to have access to them.
double hours;
double hourlyWage;
Paycheck(double hourlyWage, double hours) {
this.hourlyWage = hourlyWage;
this.hours = hours;
setPay(0);
}
hours and hourlyWage are not defined!
Uncomment this part -
/*double hours;
double hourlyWage; */