No output is displaying, cannot find error (Java) [closed] - java

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

Related

Java If Statement Trouble [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm in college and am trying to learn Java but I'm having a bit of trouble with some code. The "if (totCreditsEarned >= 180)" is erroring in my Eclipse, specifically the part in parentheses, and it's unable to give any suggestions to fix it.
import java.util.Scanner;
public class CSC161lab6_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Credits Earned: ");
String totCreditsEarned = scanner.nextLine();
System.out.println("Credits Earned is: " + totCreditsEarned);
if (totCreditsEarned >= 180) {
System.out.println("Wow!");
} else {
System.out.println("Oof!");
}
}
}
180 is an Integer. totCreditsEarned is a String.
You will need to convert totCreditsEarned to an Integer
if (Integer.valueOf (totCreditsEarned) >= 180)
...
Of course if you enter a non-integer value it will fail
The full compilation error is.
CSC161lab6_1.java:13: error: bad operand types for binary operator '>='
if (totCreditsEarned >= 180) {
^
first type: String
second type: int
1 error
It's telling you that you can't compare an int and a String using >=. You need two ints, so totCreditsEarned must be an int or converted to an int before you can compare them.
You can read it from the Scanner as an int. So no need to convert after.
int totCreditsEarned = scanner.nextInt();

"StackOverflow" error when using Recursion in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package plaindromenumbers;
import java.util.Scanner;
/**
*
* #author taha
*/
public class PlaindromeNumbers {
/**
* #param args the command line arguments
*/
int func1(int n)
{
if(n==1)
return 1;
return n*func1(n-1);
}
static boolean check=false;
int func(int no)
{
String a=""+no;
String reverse = new StringBuffer(a).reverse().toString();
if(a.equals(reverse))
{
if(!a.contains("0"))
{
System.out.println("hey");
check=true;
return Integer.parseInt(a);
}
}
// else
// {
func(no++);
if(check==true)
{
return 0;
}
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner in=new Scanner(System.in);
System.out.println("Enter testcase");
int testcase=in.nextInt();
while(testcase>0)
{
int a=in.nextInt();
PlaindromeNumbers obj=new PlaindromeNumbers();
System.out.println(obj.func(a));
testcase--;
}
}
}
Here is the problem:
func(no++);
Let's say no is 32. This will pass 32 to func and then increment it. Which means that you are once again calling the function with a value of 32. And when that function hits this line, it will, once again, pass that 32 to func. Hence, an infinite recursive loop.
You can fix this bug like this:
func(++no);
Now it will increase no before calling func. no will be 33, it will match its reverse, and all will be well.
You will need to return func(no + 1), this way your code will return the next palindrome number (which I assume is what you want your code to do?). check is not needed. Why include func1 when it's not even used by your code?
By the way, the stack overflow is caused by the infinite recursion of func.

A couple small basic questions making issues [closed]

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 6 years ago.
Improve this question
*Edited. I ended up with the following.
I was given this code.
package lab2;
/**
* Model of a basketball for use in quality control simulations.
*/
public class Basketball
/**
* Inflation status of this Basketball.
*/
private boolean isInflated;
/**
* Diameter of this Basketball.
*/
private double diameter;
/**
* Constructs an uninflated Basketball with the given diameter.
* #param givenDiameter
* the diameter for this Basketball
*/
public Basketball(double givenDiameter)
{
isInflated = false;
diameter = givenDiameter;
}
/**
* Returns the diameter of this Basketball.
* #return
* diameter of this Basketball
*/
public double getDiameter()
{
return diameter;
}
/**
* Determines whether this Basketball can be dribbled.
* #return
* true if the basketball is inflated, false otherwise
*/
public boolean isDribbleable()
{
// can be dribbled only if it is inflated
return isInflated;
}
/**
* Returns the circumference of this Basketball.
* #return
* circumference of this Basketball
*/
public double getCircumference()
{
// PI times the diameter
double result = Math.PI * diameter;
return result;
}
/**
* Inflates this Basketball.
*/
public void inflate()
{
isInflated = true;
}
}
The only change I did was chaning the Basketball class to
public class Basketball extends java.lang.Object
Then I wrote the following code and saved it in the same folder as the the one above.
public class BasketballTest {
public static void main (String[] args){
Basketball b;
b = new Basketball(4.0);
System.out.println(b.getDiameter());
}
}
The first code compiles fine, but when compiling the second I get the following error.
BasketballTest.java:5: error: cannot access Basketball
Basketball b;
^
bad class file: .\Basketball.class
class file contains wrong class: lab2.Basketball
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
I hope someone help understanding where the problems are. I am not in a class, but doing this assignment to learn Java.
The link to the assignment http://www.briannakayama.com/COMS227/Labs/Lab2/
Thanks
First, format your code well. Second, I don't see your link. Lastly, these kind of questions are not for stack overflow but I'll answer it anyway.
Package is basically like a folder that classes and other files are stored. Project is the whole thing - group of packages. You are good with what you have.
You didn't specified what kind of error this is.... BUT I'm guessing you probably didn't make basketball class. Or put correct constructor in basketball class. Or.... I don't know. Tell me what your error it is.
OH now I see it.
You were suppose to put that file in same folder as the basketballTest class. If don't, that will give you an error.(because compt have no idea what basketball is) Or you can make a whole new class and just copy the code.
1) Consider project as a directory (eg: C Drive), package as a folder in the drive and class as .java file.
2). I don't know what do you mean by Basketball b;as Basketball is not a data type and looking at the line below you are trying to assign it float value.
Probably you need something like this,
public class BasketballTest
{
double b;
BasketballTest() //Constructor
{
this.b = 4.0;
}
void display(){System.out.println(b);}
public static void main (String [] args){
BasketballTest obj = new BasketballTest();
obj.display();
}
}
That's because you don't have Basketball class inside your folder. Check your folder positions and your package declarations.
And for the question, I believe that it wants you to make variable 'b' to execute method to change isInflated to true; I'm not sure about your homework link. If you know what you are suppose to do, I can help you with that.

Why this simple algorithm for finding the maximum number (among 3 numbers) can't print out the result? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My very simple program should sort out the maximum one among 3 numbers. I have used a static method for algorithm and called that into the main method. User should enter 3 floating numbers by using a space. afetr pressing enter the program should print out the biggest floating number. But it doen't. It just shows a bunch of errors. The attachment shows what error I have:
MaximumFinder.java:
package maximumfinder;
import java.util.Scanner;
public class MaximumFinder {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
System.out.println("Enter 3 floating-pont value separated by space:");
double num1 = inScanner.nextDouble();
double num2 = inScanner.nextDouble();
double num3 = inScanner.nextDouble();
double result = maximum(num1, num2, num3);
System.out.println("Maximum number is: "+result);
}
public static double maximum(double x, double y, double z) {
double maximumValue = x;
if (y > maximumValue)
maximumValue = y;
//statement
if (z > maximumValue)
//statement
maximumValue = z;
return maximumValue;
}
}
If I exclude the package declaration it is working perfectly fine for me
Just make sure that the class is in the right package.
Note : compile & run from command line.
Why dont you simply use the max() method from Java? It may solve your problem...
EDITED
I've tried your code and it runs perfectly, but I got the same error as you, if I type the double numbers using dots, like "3.5". If I type the numbers using commas("3,5") the program is executed till the end.
I hope this help you.
public static double maximum(double firstValue, double... otherValues) {
double maximumValue = firstValue;
for (double v : otherValues) {
maximumValue = Math.max(maximumValue, v);
}
return maximumValue;
}

Finding a prime number that is one greater than a squared number from a given number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So I have this small method I am working on for class and this question is a tough one and a bit confusing. Basically what I have to to is write a method that will take a number that you input and it will find the next prime number that is 1 greater than a squared number. For example if I were to type in 10 the result would be 17, because 17 is a prime number and its 1 greater than a square, the square being 16. So I have to write a method with only one return statement in it to do this, and I have absolutely no idea where to start. What I have so far ispublic static int nextSquarePlus1Prime(int num){
} Literally. Some helped would be greatly appreciated.
I already have this method where I check if the number is prime or not:
public static boolean isPrime(int num){
int i;
boolean numberIsPrime=true;
if(num==1){
numberIsPrime=false;
}
for(i=2;i*i<=num;i++){
if(num%i==0){
numberIsPrime=false;
}
}
return numberIsPrime;
}
Is there a way to add to this method with another piece of code to work alongside with this one to check if the number is a square?
So this is what i came up with for my code and i put in 10 as my number. I'm getting 50 when I should be getting 17.
public static int nextSquarePlus1Prime(int num){
int i;
int save=0;
for(i=1;i<=num;i++){
if(isPrime(i)&& i*i+1>=num){
save=i*i+1;
}
}
return save;
}
The sqrt() method may help. Here is an idea of how to use it:
int squareRoot = (int) Math.sqrt((double) input);
Input being the number you want to round. Casting the result to an int will automatically round it. It is optional. I cast the input to a double, but you only need to do so if your input in an int.
Here is an easier way to check if a number is prime:
if (input % 2 == 0){
input += 1;
}
return input;
I reccomend you look at #useSticks answer again. Using the approach he described I created a method that does exactly what I think you want.
Here is a method that finds the square root of a positive number:
public static int sqrt(double number){
double g1;
if(number==0){
return 0;
}
double squareRoot = number/2;
do{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}while((g1-squareRoot)!=0);
return (int) squareRoot;
}
You should provide some additional details for more specific pointers.
In Java, there is a method "is probable prime" that may be of use to you.
Its easier to square a number than it is to find the square root, so it may make sense to find the square root of the starting number, then round up. Take that number and square it, add 1 and check for prime. If not, add 1 to the variable and try again.
Good luck.

Categories

Resources