Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So for this I have to create a program in Java that asks the user to input all three points of a triangle, and then I must find the sides and area. All the math must be done separately from the tester class, where I will prompt the user with the questions...
- how do I ask the user to input something in the tester class but get those integers back in the original program?
In the tester class's main method, you can make an instance of the non-tester class containing the functions where you do the math, i.e.:
TriangleMath tMath = new TriangleMath();
// where TriangleMath is the name of the other class, and "tMath" is
// an instance of it. then:
Scanner keyboard = new Scanner(System.in);
int point1 = (int) keyboard.nextLine().charAt(0);
int point2 = (int) keyboard.nextLine().charAt(0);
int point3 = (int) keyboard.nextLine().charAt(0);
int area = tMath.area(point1, point2, point3);
in this, you're making an object of the class containing all of the math functions and stuff, then getting the input in the main method of the tester class, then passing the inputs into the area function of the instance of the TriangleMath class (tMath).
The .charAt(0) turns it into a char, and the (int) casts it as an int.
I hope I was of some help!
Scanner s = new Scanner();
double x1 = s.nextDouble();
double y1 = s.nextDouble();
and so on...
and pass those variables in the functions that you have created.
I hope it will help you.
Thanks
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I'm working on a project for school and I've run into a wall where I need to get a user inputted Int and double from my main method to a required non-static method. this non-static method is supposed to be used for the calculation and printing of the answer based on the price (double) and the amount paid (int).
public static void main (String[] args){
Scanner check = new Scanner(System.in);// import of the scanner
char cents = '\u00a2';//Unicode of cent symbol
System.out.print("Your item costs (25" + cents + " minimum, Increments of 5" + cents + "): ");
Double price = check.nextDouble(); //price of the item
System.out.println("You paid (whole dollars only): ");
int paid = check.nextInt(); //amount paid
VendingChange newVend = new VendingChange();//creates a copy of the class
newVend.Secondary();// calls the nonstatic
that's the main method
public void Secondary () {
System.out.println("your change is " +/*this is where the equation is supposed to go*/ );
and this is the non-static
I've tried adding an extra static method, which eliminated the errors, but the int and double still wouldn't go through. meaning I can't use the int and double in any other method, because it doesn't recognize the names.
try replacing
Double price = check.nextDouble();
with
double price = check.nextDouble();
or even
String price1 = check.nextLine(); // gets input in form of string
double price = Double.parseInt(price1); // converts to double
Similarly, integers can also be changed:
int paid = check.nextInt();
to
String paid1 = check.nextLine();
int paid = Integer.parseInt(paid1);
I would also recommend reading (as mentioned in the comments) tutorials and really understanding the difference with static and non-static methods (instance).
(uppercase represents the class, while lowercase represents an instance)
Let's say we have a Person class. A static method would be like
Person.getPopulation(). That would return the population. This method is not instance (person) specific, rather it applies to the type of thing a person is.
An instance method would be better when you have something like person.changeName(). This would change the name of a person. An instance of a person. However, if you create it static, like Person.changeName(), which person's name would you be changing?
Think of the class Person like a type of object. People are objects (in this analogy). Person class is a type. Every person instance is every person that lives (in the program).
This same 'type of thing' vs actual 'thing' that exists under that type can be applied to a lot of things in programming, and is the basis of Object Oriented Programming (OOP).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My homework is to create a program that tells how many calories you consumed depending on the numbers of cookies you ate. But when I run the program, it doesn't show anything and I need it to work for my homework.
Here's the code for more context:
package cookie.calories;
import java.util.Scanner;
public class CookieCalories {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int TotalNumberOfCookiesPerBag = 40;
int CaloriesPerServing = 300;
int ServingsPerBag = 10;
//The next equation represent the number of cookies per servings
int NumberOfCookiesPerServings = TotalNumberOfCookiesPerBag/ServingsPerBag;
//number of calories per cookies
int CaloriesPerCookies =CaloriesPerServing/NumberOfCookiesPerServings;
Scanner ob2 = new Scanner(System.in);
System.out.println("This programm can determined how many calories you ate depending on the number of cookie you ate.");
//Below this sentence, the amount of calories will be determined
System.out.println("Enter the number of cookies you ate");
int Cookies = ob.nextInt(4);
int TotalCaloriesConsumed = CaloriesPerCookies*Cookies;
System.out.println(TotalCaloriesConsumed + "for the amount of cookies you ate");
}
}
The program shows me that I didn't made any error in the way I wrote my program but when I play it, it doesn't show anything in the output. Why?
When I run your program I get this output
This programm can determined how many calories you ate depending on the number of
cookie you ate.
Enter the number of cookies you ate
when I enter (for example) 4 and press enter I get this error
Exception in thread "main" java.util.InputMismatchException: For input string: "4"
under radix 4
at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
at testasdf.Testasdf.main(Testasdf.java:38)
Not really sure why you have "ob.nextInt(4);" instead of "ob.nextInt();" should change your code to be nextInt();
So I am unsure of why nothing is showing up for you, are you sure you have a output screen set up?
What IDE are you using?
Remove the '4' from the nextInt method params.
int Cookies = ob.nextInt();
Also you don't need another Scanner object, you can use the same one to get input from the user multiple times, so you can remove this from your code.
Scanner ob2 = new Scanner(System.in);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to write a Tester and a class that can solve quadratic function.
If you are unfamiliar with quadratic function, or need a reminder, here is a quick link to it's Wikipedia article: http://en.wikipedia.org/wiki/Quadratic_function
My tester seems to work fine, however whenever I try to call the method, the program stops, and it doesn't display the output of the method (which is supposed to display the answer).
I am not skilled enough to find out if the error is within the class or the tester.
Tester:
/**
* A Tester to use to solve quadratic formula. Enter your values
* when prompted, and the answer will be displayed on screen.
*
* #author (Austin C.)
* #version (1.0.0)
*/
import java.io.*;
import java.util.*;
public class C_tester
{
public static void main (String args[])
{
Scanner kb = new Scanner(System.in);
System.out.println ("Enter the coefficents in the form of the following:\n1.A\n2.B\n3.C");
System.out.print("Enter the number for A:");
int a = kb.nextInt();
System.out.print("Enter the number for B:");
int b = kb.nextInt();
System.out.print("Enter the number for C:");
int c = kb.nextInt();
QuadraticFunction.quadratic(a,b,c);
}
}
Quadratic Function Class:
/**
* #Params: you must enter the coefficents, A, B, and C, and the program will calculate them to find the answer
* to a quadratic forumla. coefficents must be integers or doubles.
*
* #author (Austin C.)
* #version (1.0.0)
*/
public class QuadraticFunction
{
public void QuadraticFunction()
{
}
public static double quadratic(double a, double b, double c)
{
double topPos;
double topNeg;
double bot;
topPos = -b + Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
topNeg = -b - Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
bot = 2*a;
double ansPos = topPos/bot;
double ansNeg = topNeg/bot;
return ansPos + ansNeg;
}
}
Any help to find the error myself or find it for me is greatly appreciated. Also, if you find a more efficient way to do this, please share! I am always looking for more efficient ways to write code.
If the question is unclear, please say so and I can redo it in a more understandable way.
quadratic has return type double, and there are no print statements in the method. What this means is that it should return a value, but there is no reason that value should be printed. You can fix this by assigning the function result to a variable, and then adding a statement to print it to screen, like so:
double answer = QuadraticFunction.quadratic(a,b,c);
System.out.println(answer);
Also, you should note that a quadratic equation can have 2 real roots (possibly both the same), so you should be returning an array of doubles rather than a single double which is the sum of those 2 roots.
What do you expect it to print? You never tell the program to print anything. You can have it print the result to stdout by changing the last line in the tester to System.out.println(QuadraticFunction.quadratic(a,b,c)); if the program stops and there was no stack trace printed it means that it was executed succesfully
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
can anyone tell me how can i connect one string with a group of strings and each string have certain value.
for exampe:
String car = new String()
//i give it a value of say 10 dollars
and then i have this group of Strings/Objects
for example:
String frontWindow = new String()
//i set a value for it
String backWindow = new String()
//i set a value for it
String wheel = new String()
//i set a value for it
And i want to calculate the sum of the car with some or all of the string above.I thought about usint the print method but then i would be able to choose only all strings.So does anyone know how can i make this work in the console output?
That's not very clear, but it seems like you want to make a Car class and access variables inside of it.
public class Car{
public String frontWindow, wheel;
public Car(String frontWindow, String wheel){
this.frontWindow = frontWindow;
this.wheel = wheel
}
}
then, from inside an other class you can access it like this:
Car myCar = new Car("this is my front window", "this is my wheel");
System.out.println(myCar.wheel);
if you want those variables to hold values instead of strings, make them "double"s or "int"s.
Look up the difference between storing Strings (text) and Numbers (int, double, long, float, etc.)
Also look up Object Oriented programming for how that all works. Try a google search for "how to create a new object in java", or something like that. Then you also might be able to use getters and setters for those variables you want.
You can combine strings like so:
String totalSum = car + frontWindow + backWindow + wheel;
However, given this example:
String car = "10.00";
String frontWindow = "2.00";
String backWindow = "2.00";
String wheel = "1.00";
They would add together end-to-end and be like so: "10.002.002.001.00"
int and double are actually meant to have number operations done to them instead of String.
int i = 5; // int's are whole numbers, no decimals allowed
int j = 3;
int total = i + j; //Total is 8
double d1 = 1.1;
double d2 = 2.5;
double dTotal = d1 + d2; //Total is 3.6
You can still print them out to the console by doing System.out.println(dTotal);
Probably out of scope, but if you plan on dealing with money in a serious business app, you should check into the BigDecimal class because of tiny rounding errors that can occur with doubles and floats.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the code, where are the run time errors? I tried and tried to find it but I just could not, does anyone else see where these errors could be within the code?
public class HasErrors
{
public static void main(String [] args)
{
int x = 0;
int y = 0;
Scanner in = new Scanner("System.in");
x = in.readInt();
System.out.print("Please enter another integer: ");
x = in.readInt();
System.out.println("The sum is " + x + y);
}
}
If you're instantiating a new Scanner with System.in it shouldn't have quotes around it:
Scanner in = new Scanner(System.in);
See here.
Well if a user enters in somethign other than a number, say Hello World, then readInt() will throw an InputMismatchException. Also the Scanner class does not have a readInt function. I think you meant to put nextInt.
Along with the readInt errors already mentioned, X and Y will be concatenated in the println statement rather than added. This could be fixed by putting the x + y in parentheses.