can somebody correct me what i did wrong here? my goal was for the user to input a number on the parameter System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
double upgradeAccessories= sc.nextDouble(); but i can't seem to see the issue
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("#####");
Scanner sc = new Scanner(System.in);
double availAmount;
Car owner = new Car();
owner.owner("Marcus Laurence", 2014);
double availAmount1=owner.upgradeAccessories(availAmount);
double remainAmount=owner.upgradeAccessories(availAmount);
System.out.println("Owner:" + owner.name);
System.out.println("Model:" + owner.model);
System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
double upgradeAccessories= sc.nextDouble();
System.out.println("Installed AC:" + owner.hasAC);
System.out.println("Installed Leather Seats:" + owner.hasLeatherSeats);
System.out.println("Installed Back Wipers:"+ owner.hasBackWipers);
System.out.println("Installed Fog Lights:" + owner.hasFogLights);
System.out.println("Amount Remaining After Upgrade:" + df.format(remainAmount));
}
public double upgradeAccessories(double availAmount)
{
if(availAmount == 25000)
{
availAmount -= 21500;
hasAC=true;
}
else if(availAmount == 40000)
{
availAmount -= 21500;
availAmount -= 14400;
hasAC=true;
hasLeatherSeats=true;
}
else if(availAmount == 50500)
{
availAmount -=21500;
availAmount -=14400;
availAmount -=6250;
availAmount -=3300;
hasAC=true;
hasLeatherSeats=true;
hasBackWipers=true;
hasFogLights=true;
}
return availAmount;
}
how do i make the user input a double?
You did this part fine - scanner.nextDouble() is all it takes. Here's a really simple standalone example, including output:
System.out.print("enter a double: " );
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
System.out.println("d: " + d);
enter a double: 123.456
d: 123.456
So reading a double is not the issue. However, this line is suspicious:
double upgradeAccessories = sc.nextDouble();
Why? A few reasons..
It defines a new variable "upgradeAccessories" which is not used anywhere else – so it's reading a value (from sc.nextDouble()) and then doing nothing with it. This is something that an IDE (such as IntelliJ or Eclipse) will help you see – it will draw attention to unused code, like declaring a value that is never used.
The name of the variable – "upgradeAccessories" – is the same as the method name defined elsewhere in your code, namely:
public double upgradeAccessories(double availAmount)
So to fix your code, it seems that you probably want to replace this:
double upgradeAccessories = sc.nextDouble();
with something like this:
double next = sc.nextDouble();
owner.upgradeAccessories(next);
Also, the way you're comparing doubles is dangerous. Generally, fractional numbers are never exactly equal. Instead, of doing comparisons like this:
if (availAmount == 25000) { ... }
It's better to do something like greater-than-or-equal:
if (availAmount >= 25000) { ... }
Or if you're using only whole-number values, use integer instead of double, and then direct x == y comparisons will work fine.
In this segment of your code...
double availAmount;
Car owner = new Car();
owner.owner("Marcus Laurence", 2014);
double availAmount1=owner.upgradeAccessories(availAmount);
... you passed the "availAmount" in to a function as a parameter. You haven't given that variable any value yet. You just declared it. If you assign a default value to it like 0.0, does it work?
Related
Hello I'm working on a project for my java class, I'm supposed to write a code for a Algebra tutor that goes like this:
Write a program with a that displays a randomly generated problem that asks the user to solve for the y variable, takes input from the user, and prints "correct" if the user answered correctly and prints "incorrect" if not. Your main should give one problem and then exit. Use one or more methods to produce this behavior.
This is regarding the formula mx + b. So here is what I have so far, and works!
import java.util.Random;
import java.lang.Math;
import java.util.Scanner;
class Main {
public static void main(String[] arg){
double min_value = -100;
double max_value = 100;
double m_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
double x_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
double b_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("b = " + b_value);
System.out.print("What is the value of y? ");
Scanner user_input = new Scanner(System.in);
String user_answer = "";
user_answer = user_input.next();
int correct_answer = (int)m_value * (int)x_value + (int)b_value;
if (user_answer.equals(correct_answer))
System.out.println("You are correct!");
else
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer);
}
}
so even tho the output is correct, I need to break down the code into smaller methods, this is where Im getting confused on how to take a piece of that code and put it in another method that once it runs it calls for that method too and gives me the same output. I been ready the material given but the more I read it the more confuse I get. If anybody has any ideas or suggestions please let me know any info will be really appreciate. Thank you
Here's a quick rundown on methods, so it's not completely done yet. Ask, if you need more help! Good luck on your homework and on becoming one of the beast developers!
public class Main {
public static void main(String[] args) {
int a = 1; // give a value of 1
methodTwo(a); // sending the int a into another method
}
// Here's method number two
static void methodTwo (int a) { // it gives a's type and value
System.out.println(a); //Gives out a's value, which is 1
}
}
Technically you've solved the problem correctly, you are using one or more methods, but perhaps what you trying to do is a common code refactor called the extract method / extract function refactor Executing this type of refactor leads to much more readable and maintainable code, and is easy to do.
As a starter, identify code that repeats or looks similar, in your case, the following lines look ripe for extract method:
double m_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
double x_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
double b_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;
Notice that the RHS of each line is identicial, so we can replace the explicit code with a method call like this:
double m_value = getRandomDoubleBetween(max_value, min_value);
double x_value = getRandomDoubleBetween(max_value, min_value);
double b_value = getRandomDoubleBetween(max_value, min_value);
private double getRandomDoubleBetween(double max_value, double min_value) {
return (int)(Math.random()*((max_value-min_value)+1))+min_value;
}
You can identify other areas of code that either contain repetition or perhaps some hard to understand code that would be more understandable if it was extracted into a method that had a name that reveals what the code is doing.
Please review this, you are comparing string with integer,
if (user_answer.equals(correct_answer))
This may help you:
import java.util.Scanner;
class Main {
public static void main(String[] arg) {
double min_value = -100;
double max_value = 100;
double m_value = generateRandom(max_value, min_value);
double x_value = generateRandom(max_value, min_value);
double b_value = generateRandom(max_value, min_value);
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("b = " + b_value);
checkAnswer(m_value, x_value, b_value);
}
private static void checkAnswer(double m_value, double x_value, double b_value) {
System.out.print("What is the value of y? ");
Scanner user_input = new Scanner(System.in);
String user_answer = "";
user_answer = user_input.next();
int correct_answer = (int) m_value * (int) x_value + (int) b_value;
if (user_answer.equals(String.valueOf(correct_answer))) {
System.out.println("You are correct!");
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer);
user_input.close();
}
}
static int generateRandom(double max_value, double min_value) {
return (int) ((int) (Math.random() * ((max_value - min_value)
+ 1)) + min_value);
}
}
This is my code as of right now: http://ideone.com/PfXNQc
import java.util.Scanner;
public class Temperature
{
public static double convertFtoC(double F)
{
return (F-32)*5/9;
}
public static void main(String[] args)
{
double F; //Stores Farenheit
double C; //Stores Celsius
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
F = keyboard.nextDouble(); //Stores Farenheit
System.out.println("The temperature in Celsius is: " + convertFtoC(F)); //Displays Celsius (call convertFtoC(F) where celcuis conversion needed)
F = 0;
System.out.println("Fahrenheit\t Celsius");
while (F <= 100)
{
// Display the F and C in increments of 10.
System.out.println(F + "\t\t" + convertFtoC(F));
// Increment for the next day.
F++;
}
}
}
I would like the loop for conversions to go up in increments of 10 rather than by 1.
Right now it is outputting this:
But I would like it to be
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
what if you write as follows.
while (F <= 90) //Last number before 100 is 90
{
// Display the F and C in increments of 10.
System.out.println(F + "\t\t" + convertFtoC(F));
// Increment for the next day.
F=F+10;
}
Additionally,why you use capitals for variables, per conventions you should start variable names with lower case letters, objects names are started with capitals.
Modern Idiomatic Professional Approach
Q32835243.java
package com.stackoverflow;
import java.util.Scanner;
public class Q32835243
{
private static double farhenheitToCelsius(final double fahrenheit) {return (fahrenheit - 32) * 5 / 9;}
public static void main(final String[] args)
{
final Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: ");
if (keyboard.hasNextDouble())
{
final double fahrenheit = keyboard.nextDouble();
final double celsius = farhenheitToCelsius(fahrenheit);
System.out.format("The temperature in Celsius is: %8.4f%n", celsius);
System.out.format("Fahrenheit\t Celsius%n");
for (int i=0; i < 100; i+=10)
{
System.out.format("%8.4f\t%8.4f%n", fahrenheit + i, farhenheitToCelsius(fahrenheit + i));
}
}
else
{
System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
System.exit(1);
}
}
}
Link to this solution as a fork of what you did.
I put this working code on ideone.com so you could easily see the differences and how to make this trival code bullet proof as possible.
Things to take note of in this solution:
Always make as many things final as possible. It eliminates two of the most common classes of program errors NullPointerException and side effects.
Always limit the scope of variable references and visibility of methods as much as possible. You want to define variables when you use them and make as many things private as you can.
Always name your variables in the context that you are using them so that you do not need to put comments explaining what they are.
Comments should only be in clean code when you are deviating from the expected. All your comments were useless tautology and violate the DRY principle as well. If things change now you have to update the code and the comments. More to maintain, easier to just name things that are self explanatory.
Using single letter variable names in short functions is fine. In the context of the block f and c would be acceptable names for fahrenheit and celsius if they were object references. But in the case of homework explicit is always better for communicating exactly what you are thinking.
Always follow the style guide when using lowerCamelCase and UpperCamelCase. Know when to use each in the correct context.
Always check your inputs and handle invalid data. Never try and fix it, if it is wrong stop the program, explain to the user where they went wrong and end the program. Defensive programming is terrible.
Always use the features of the different objects to your advantage. Use the String.format() and PrintStream.format() to eliminate formatting issues and making things more maintainable.
Learn to use the Scanner class correctly. the .hasNextXxx() methods are there for a reason, use them!
The scanner class has a terrible API and is the source of more frustration to use programmers than just about anything else.
For enhanced readability:
Don't use Capitalized identifiers for the variables as they are meant for the constants in Java.
Use meaningful identifier names.
Here is the code:
import java.util.Scanner;
public class Temperature
{
public static double convertFarenheitToCelsius(double farenheit)
{
return (farenheit-32)*5/9;
}
public static void main(String[] args)
{
double farenheit; //Stores Farenheit
double celsius; //Stores Celsius
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
if (keyboard.hasNextDouble()){
F = keyboard.nextDouble(); //Stores Farenheit
System.out.println("The temperature in Celsius is: " + convertFarenheitToCelsius(farenheit)); //Displays Celsius (call convertFarenheitToCelsius(farenheit) where celcius conversion needed)
farenheit = 0;
System.out.println("Fahrenheit\t Celsius");
while (farenheit <= 100){
System.out.println(farenheit + "\t\t" + convertFarenheitToCelsius(farenheit));
farenheit += 10;
}
} else {
System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
System.exit(1);
}
}
}
Set F+=10 instead of F++ in the loop.
I think in your case use FOR is a better choice.
public static void main(String[] args) {
double fahrenheit = 50;
double increment = 10;
for(;fahrenheit <= 100; fahrenheit += increment){
System.out.println(fahrenheit);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
To practice my basic programming skills over the summer I decided to write a 1dimensional motion physics problem solver. I am getting a java.lang.Nullpointerexception error whenever I try to run the program. I can't figure out what I've written incorrectly to give me the error. NOTE: Right now I am assuming the input for the solveFor variable will be "acceleration" for the sake of fixing this error:
import java.util.Scanner;
public class PhysicsProblem
{
private double vI; // initial velocity
private double vF; // final velocity
private double t; // time
private double deltaX; // change in the x value
private double accel;
private String missingVar;
public PhysicsProblem (double acceleration, double initialV, double finalV, double time, double changePosition)
{
acceleration = accel;
initialV = vI;
finalV = vF;
time = t;
changePosition = deltaX;
}
Scanner scan = new Scanner(System.in);
public void getUnknownsAccel()
{
//-----------
// checks for another unknown value that is not accel
//-----------
if (missingVar.equalsIgnoreCase("time"))
{
System.out.println("Please enter the value for time: ");
t = scan.nextDouble();
while (t <= 0 || !scan.hasNextDouble())
{
System.out.println("That is not an acceptable value!");
t = scan.nextDouble();
}
}
if (missingVar.equalsIgnoreCase("initial velocity"))
{
System.out.println("Please enter the value for initial velocity: ");
vI = scan.nextDouble();
while (!scan.hasNextDouble())
{
System.out.println("That is not an acceptable value!");
vI = scan.nextDouble();
}
}
if (missingVar.equalsIgnoreCase("final velocity"))
{
System.out.println("Please enter the value for final velocity: ");
vF = scan.nextDouble();
while (!scan.hasNextDouble())
{
System.out.println("That is not an acceptable value!");
vF = scan.nextDouble();
}
}
if (missingVar.equalsIgnoreCase("delta X"))
{
System.out.println("Please enter the value for delta X: ");
deltaX = scan.nextDouble();
while (!scan.hasNextDouble())
{
System.out.println("That is not an acceptable value!");
deltaX = scan.nextDouble();
}
}
}
This is the class file for the program. I'm getting an error in the line 36:
"if (missingVar.equalsIgnoreCase("time"))"
As well as getting an error in line 40 of the main program body:
"problem1.getUnknownsAccel();"
public static void main (String[] args)
{
String missingVar; // other missing variable
double vI = 0;
double vF = 0;
double t = 0;
double deltaX = 0;
double accel = 0;
Scanner scan = new Scanner(System.in);
PhysicsProblem problem1 = new PhysicsProblem (accel, vI, vF, t, deltaX);
System.out.println("Which variable are you solving for? ");
String solveFor = scan.nextLine();
// after receiving solveFor input, assesses data accordingly
if (solveFor.equalsIgnoreCase("acceleration"))
{
System.out.println("Solving for Acceleration!");
System.out.println("Are there any other unknowns? (enter 'none' or the name " +
"of the variable)");
missingVar = scan.nextLine();
do
{
problem1.getUnknownsAccel();
System.out.println("Are there any other unknowns? (enter 'none' or the name " +
"of the variable)");
missingVar = scan.nextLine();
}
while (!missingVar.equalsIgnoreCase("none") || !missingVar.equalsIgnoreCase("acceleration"));
if (missingVar == "none");
{
// Write code for finding solutions
System.out.println("Assuming you have given correct values, the solution is: ");
}
}
Why is it throwing an exception?
missingVar is obviously null. Look back in the code to find out why. And doing this, ask yourself where do you ever give the variable a String in the PhysicsProblem class?
Answer: you don't!
Note that two variables with the same name declared in different scopes are not the same variable. Just because your two classes have a missingVar String variable does not mean that they share the same variable, and as you're finding out, they in fact don't. The solution: set the missingVar variable in the PhysicsProblem class before trying to use it. Give the class a setter method for this.
i.e.,
public void setMissingVar(String missingVar) {
this.missingVar = missingVar;
}
And then call the method before using the variable.
You never initialize missingVar to anything, so it's null. You need to assign it something so it's not null.
Incidentally, you can switch the order in your call to avoid a NullPointerException here:
while (!"none".equalsIgnoreCase(missingVar) ||
!"accelmissingVar".equalsIgnoreCase(missingVar));
Also, on this line
if (missingVar == "none");
Remove the semicolon, because that semicolon is interpreted to be the body of the if block, causing your actual block below to not be associated with if (it would then always be executed, regardless of the condition in your if).
Don't compare string values with ==, which compares two objects references to see if they refer to the same object. Use the equals method:
if ("none".equals(missingVar))
I'm trying to solve exercise from Art & Science of Java, solution for quadratic equation.
import acm.program.*;
public class QuadraticEquation extends ConsoleProgram {
public void run(){
println("Enter coefficients for quadratic equation");
int a = readInt("Enter first number: ");
int b = readInt("Enter second number: ");
int c = readInt("Enter third number: ");
double result = quadratic(a,b,c);
println("The first solution is: " + result);
}
private double quadratic(int a, int b, int c){
double underSquare = (b*b-4*a*c);
double x = (-b+Math.sqrt(b*b-(4*a*c)))/(2*a);
if (underSquare < 0) {
return null;
} else {
return (x);
}
}
}
I have an error in line:
return null;
saying:
Type mismatch: cannot convert from null to double
I don't really understand what this error, how should I solve this correctly?
You need to understand the difference between primitive types (e.g., double) and boxed object types (e.g., capital-D Double). Whereas Double is a reference to an object (and hence can be null), double is a primitive and can not be null.
The compiler is telling you that you declared the return type of your function to be double and so null can not be converted to a double.
Of course you could "fix" the problem by changing the return type to Double (which would make the null return legal, and cause your primitive doubles to be
auto-boxed into Doubles) but that would not really serve you well. You want a better error handling strategy (of which there are many ... some possibilities are to throw an exception, use an Optional return type, use a flag value such as not-a-number aka Double.NaN).
You are asserting that the method should return a primitive of type double, instead you are returning a null value, which doesn't match the stated contract of the method.
What you want to do here depends entirely on how you want to catch this kind of error and there are a number of different solutions here that would be correct, but throwing a null object isn't one of them.
It's also worth noting that in your shown example:
double underSquare = (b*b-4*a*c);
double x = (-b+Math.sqrt(b*b-(4*a*c)))/(2*a);
if (underSquare < 0) {
return null;
} else {
return (x);
}
should be:
double underSquare = (b*b-4*a*c);
double x = (-b+(Math.sqrt(underSquare)))/(2*a);
return x;
which is equivalent to:
double underSquare = (b*b-4*a*c);
double x = (-b+Math.sqrt(b*b-(4*a*c)))/(2*a);
if (underSquare < 0) {
return double.NaN;
} else {
return (x);
}
but far more readable.
Java is smart enough to know that it shouldn't be taking the square root of a negative number and so if you just return x in both cases the code should run just fine. That said, you should also try to catch any exception here. Since there are a lot of error that could occur in directly evaluating numbers you should try something of this sort:
double underSquare = (b*b-4*a*c);
try{
double x = (-b+(Math.sqrt(underSquare)))/(2*a);
} catch (Exception e){
throw e;
} finally {
return x
}
return x;
Since you seem to will to compute the real solutions of the equation you should probably consider throwing an exception.
You cannot return null because it can be used only with references while you want to return a double, which is a primitive data type
You have few problems in your code from returning null to a double method...I have revised your program compare it against yours and if there is any part that you have questions then add a comment to my answer and I'll do my best to help you.
public static void main(String[] args) {
run();
}
static void run(){
System.out.println("Enter coefficients for quadratic equation");
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter first number: ");
Double a = keyboard.nextDouble();
System.out.println("Enter second number: ");
Double b = keyboard.nextDouble();
System.out.println("Enter third number: ");
Double c = keyboard.nextDouble();
resultParta(a,b,c);
}
public static void resultParta(Double a, Double b, Double c) {
double discriminant = Math.pow(b,2) - 4*a*c;
double answer1 = (-b + Math.sqrt(discriminant))/(2*a);
double answer2 = (-b - Math.sqrt(discriminant))/(2*a);
if(discriminant > 0)
{
System.out.println("answer1: " + answer1);
System.out.println("answer2: " + answer2);
}
else if(discriminant == 0)
{
System.out.println("answer2: " + answer1);
}
else
{
double root1complex = -b/(2*a);
double root1complex2 = Math.sqrt(-discriminant)/(2*a);
System.out.println(root1complex + " + " + root1complex2 + " i ");
System.out.println(" ");
System.out.println(root1complex + " - " + root1complex2 + " i ");
}
}
Why do you check for negative determinant after you take the square root? That makes no sense. Check before you do it.
You return a single double, in spite of the fact that quadratics have two roots. What about the one you don't return?
In that case the results are two complex conjugate roots. You could certainly write a Complex class and return both roots.
You don't check for special cases (e.g. a = 0, b = 0, c =0). You should.
Your equation is implemented in the naive style, ignoring numerical issues.
Bad implementation. Lots of room for improvement.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 10 years ago.
Please forgive my ignorance, I'm a beginner.
Can anyone tell me why I am getting my error code in the if/else statement below? I think my result for the ln multiplication is is not exactly the same as the standard multiplication (there is a .00000000006 of a difference or something like that). Is there a work around for this? I have tried to use DecimalFormat but to no avail.
If I add:
DecimalFormat fmt = new DecimalFormat ("0.###");
to the tester and
if (fmt.format(z) != fmt.format(result)){
to the if statement I receive my own same error statement. What is wrong!?
Thank you very much.
Joe
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;
public class Logs {
//integer x field & encapsulation
private static double x;
// x encapsulation
public double getX(){
return x;
}
public void setX (double ex){
if (ex >= 0){
x = ex;
}
else{
System.out.println("You may not choose a negative number, 'x' = 0");
}
}
//integer y field
private static double y;
// y encapsulation
public double getY(){
return y;
}
public void setY (double why){
if (why >= 0){
y = why;
}
else{
System.out.println("You may not choose a negative number, 'y' = 0");
}
}
//tester
public static void main (String [] args){
Logs sample =new Logs();
Scanner var = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat ("0.###");
sample.setX (var.nextDouble());
sample.setY (var.nextDouble());
x = sample.getX();
y = sample.getY();
double z = (x*y);
double logX = Math.log(y);
double logY = Math.log(x);
double logZ = (logX +logY);
double result = (Math.exp(logZ));
if (z != result){
System.out.printf("%s", "Incorrect answer: be a better coder");
}
else {
System.out.printf("%s %.3d %s %.3d %s %.3d",
"The product of the values you chose is: ", + z,
"\nln(x) + ln(y) is: ", + logZ,
"\nCompute to e: ", + result);
}
}
}
You're comparing string references rather than their values, and you want String.equals() e.g. z.equals(result)
However, I think what you're trying to do is compare two decimal numbers to a certain precision. It's more intuitive to calculate the difference and determine if that's within an acceptable error bound e.g.
if (Math.abs(z - result) < 0.01) {
// ok
}
See Math.abs(double a) for more details
I'd suggest to try
if (!fmt.format(z).equals(fmt.format(result))) {
Use BigDecimal instead of double
Use StrictMath instead of Math
I think it's because of double. For best practice use BigDecimal instead.
Please, take a look at "Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?".