I need some help with a program that i am trying to create. This is a Quadratic Equation Formula, where i have 2 classes.
The only issue that i am getting is code
"QuadraticEquation Equation = new QuadraticEquation(a, b, c);"
I am getting the error that says:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type TestQuadraticEquation is accessible. Must qualify the allocation with an enclosing instance of type TestQuadraticEquation (e.g. x.new A() where x is an instance of TestQuadraticEquation).
at TestQuadraticEquation.main(TestQuadraticEquation.java:12)
This error is occurs at line 12 (code above) and i need to find out what is wrong with that section.
public class TestQuadraticEquation
{
public static void main (String [] args)
{
java.util.Scanner scan = new java.util.Scanner(System.in);
System.out.println("Please enter the coefficients a, b and c in the order: ");
double a = scan.nextDouble();
double b = scan.nextDouble();
double c = scan.nextDouble();
QuadraticEquation Equation = new QuadraticEquation(a, b, c);
if (Equation.getDiscriminant() > 0)
{
System.out.println("The roots of the equations are " + Equation.getRoot1()
+ " and " + Equation.getRoot2());
}
else
{
System.out.println("The equation has no roots.");
}
}
class QuadraticEquation
{
private double a;
private double b;
private double c;
QuadraticEquation()
{
a = 0;
b = 0;
c = 0;
}
QuadraticEquation (double newA, double newB, double newC)
{
a = newA;
b = newB;
c = newC;
}
public double getA()
{
return a;
}
public double getB()
{
return b;
}
public double getC ()
{
return c;
}
public double getDiscriminant()
{
return (Math.pow(b,2) - 4 * a * c);
}
public double getRoot1()
{
return ((-b + Math.sqrt(getDiscriminant())/(2 * a)));
}
public double getRoot2()
{
return ((-b - Math.sqrt(getDiscriminant())/(2 * a)));
}
}
}
Here you are trying to create an instance of inner class which is QuadraticEquation class. QuadraticEquation class lies inside TestQuadraticEquation so, in order to create instance you can either declare your QuadraticEquation as static class please refer to the link: problem creating object of inner class in java
Other choice is to seperate the class such that QuadraticEquation.java and move the code of QuadraticEquation class there. That way it is no longer inner class.
Also, the other choice would be like the compiler suggested you create instance of TestQuadraticEquation and then from there you can create new object of QuadraticEquation which can be done by:
QuadraticEquation Equation = new TestQuadraticEquation(). new QuadraticEquation(a, b, c);
Related
Hello everyone I'm currently creating a simple program. I have 2 classes the first one is calculator and the second one is parameters_return. What I want to happen is that when I read the values of x and y in my second class i want to use them in my first class Unfortunately, I can't run the program because it has an error. The code is below please help me with regards to this matter. I'm just self studying I really want to learn Java.
Code in (first class) calculator class is:
class calculator {
//with parameters with return type
int add(int a, int b) {
return (a + b);
}
//with parameters without return type
void sub(int a, int b) {
System.out.print(a - b);
}
//without parameters with return type
int mul() {
parameters_return s1 = new parameters_return();
int c = (s1.x) * (s1.y);
return c;
}
//without parameters without return type
void div() {
parameters_return s2 = new parameters_return();
int c = (s2.x) / (s2.y);
System.out.println("Division = " + c);
}
}
Code in my (second class) parameters_return class is:
class parameters_return {
int x, y;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
calculator perform = new calculator();
//addition
int z = perform.add(x, y);
System.out.println("Added value = " + z);
//subtraction
System.out.println("Subtracted value = ");
perform.sub(x, y);
//multiplication
z = perform.mul();
System.out.println("Multiplication value = ");
//division
perform.div();
}
}
Is there any way to get values from main class and can be used in another class?
import java.util.*;
class ParametersReturn {
static int x, y;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
Calculator perform = new Calculator();
//addition
int z = perform.add(x, y);
System.out.println("Added value = " + z);
//subtraction
System.out.println("Subtracted value = ");
perform.sub(x, y);
//multiplication
z = perform.mul();
System.out.println("Multiplication value = " + z);
//division
perform.div();
}
}
This should be your ParametersReturn Class and make sure you should start your class name with capital letter , you are using Scanner class to use it you have to import java.util package. And to use these variables in Calculator class make these variables static
class Calculator {
//with parameters with return type
int add(int a, int b) {
return (a + b);
}
//with parameters without return type
void sub(int a, int b) {
System.out.print(a - b);
}
//without parameters with return type
int mul() {
ParametersReturn s1 = new ParametersReturn();
int c = (s1.x) * (s1.y);
return c;
}
//without parameters without return type
void div() {
ParametersReturn s2 = new ParametersReturn();
int c = (s2.x) / (s2.y);
System.out.println("Division = " + c);
}
}
And in multiplication you forgot to print the value of z
currently doing an assignemnt but i'm new to programming so was wondering how you add a value to a variable in a different class which already has an existing class
class OtherClass {
int a;
}
public class Main Class{
public static void main(String[] args) {
int b = 7;
OtherClass temp = new OtherClass();
OtherClass.a = 5
OtherClass.put(b) //this is where I'm not sure how to add b to a
}
Actual Code
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter amount of money you have: ");
Scanner input = new Scanner(System.in);
Wallet bettersWallet = new Wallet();
bettersWallet.moneyAvailable = input.nextDouble(); //then had a function which played out a bet and added/took away winnings from the bet
int winnings = 5;
bettersWallet.moneyAvailable +=winnings; //Will setMoneyAvailable function work in this scenario aswell?
}
class Wallet {
double moneyAvailable;
double openingCash;
public void setMoneyAvailable()
{
moneyAvailable += ChuckALuckDiceGame.winnings;
}
int b = 7;
OtherClass temp = new OtherClass();
temp.a = 5;
temp.a += b; //Same as temp.a = temp.a + b;
System.out.println(temp.a);
What we are doing here,
We are creating an object of class OtherClass, the name of the object is temp.
Then we are assigning the value 5 in the attribute a of object temp
Then we are adding the value of primitive variable b into the variable temp.a.
The sum of the above equation is being assigned to the value of temp.a
Then I am printing the sum at the end through System.out.println(temp.a);
The purpose of this program is to print
the number of solutions and the solution(s) to a quadratic function,
entered by the user.
The problems here is that I get errors for having private variables,
why is this? Also for my constructor it states that my variables cannot be resolved even though
they are established in the main method. Finally, my variables will pass to the main method or to the "toString" method for use
in the main method.
This is for a school assignment and my professor requires that I use the "toString" method as well as have private variables. I apologize in advance for any formatting mistakes and for the large question, for I am new
to this site.
import java.util.Scanner
public class QuadraticSolver
{
private static Scanner in;
public static void main(String[]args)
{
QuadraticSolver qs = new QuadraticSolver();
in = new Scanner(System.in);
private double a;
private double b;
private double c;
System.out.println("Enter coefficients for quadratic function. ");
a = in.nextDouble();
b = in.nextDouble();
c = in.nextDouble();
qs.getRoot1(a,b,c);
qs.getRoot2(a,b,c);
qs.numOfSolutions(a,b,c);
System.out.println("Your quadratic function is " + a + "x^2 + " + b + "x + " + c);
System.out.println(qs.toString());
}
public QuadraticSolver()
{
a = 0;
b = 0;
c = 0;
}
public double getRoot1(double a,double b,double c)
{
private double root1;
root1 = (-b + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a);
return root1;
}
public double getRoot2(double a,double b,double c)
{
private double root2;
root2 = (-b + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a);
return root2;
}
public int numOfSolutions(double a,double b,double c)
{
private int sol = 0;
private double d;
d = Math.pow(b,2) - 4*a*c;
if(d > 0)
{
sol = 2;
}
else if(d == 0)
{
sol = 1;
}
else if(d < 0)
{
sol = 0;
}
return sol;
}
public String toString()
{
return "There are(is) " + sol + " solution(s). x = " + root1 + " x = " + root2;
}
}
You should move the variables to the class level:
public class QuadraticSolver {
private double a;
private double b;
private double c;
...
}
Next, you need to access the variables that belong to the object qs:
public static void main(String[]args)
{
QuadraticSolver qs = new QuadraticSolver();
in = new Scanner(System.in);
System.out.println("Enter coefficients for quadratic function. ");
qs.a = in.nextDouble();
qs.b = in.nextDouble();
qs.c = in.nextDouble();
...
}
And finally, you should remove the variables from the methods getRoot1(), etc. This is because each method can access the variables a,b,c belonging to the object itself. Thus:
public double getRoot1()
{
double root1 = (-b + Math.sqrt(Math.pow(b,2) - 4*a*c)) / (2*a);
return root1;
}
And at the call sites:
public static void main(String[]args)
{
...
double root1 = qs.getRoot1();
double root2 = qs.getRoot2();
int numsol = qs.numOfSolutions();
...
}
Private instance variables belong to a class. When you create an object that is an instance of a class, the object may have instance variables that belong to the object; if you create multiple instances of a class, each of those instances (objects) has its own set of instance variables. You declare those in the class:
public class QuadraticSolver
{
private static Scanner in;
private double a; // instance variables
private double b;
private double c;
Now, to access those variables, you need to have an object. If you're in a non-static method inside the class, or in a constructor for the class, the method will work with an object called this, so you could say this.a or just a to get at that variable. If you're outside the class, or in a static method in the class (including main), you need to tell it what object you're working with. In your program, your main method has an object qs that is a QuadraticSolver, and you can get at that object's instance variables by saying qs.a, qs.b, etc.
Variables declared inside a method are local variables. They're for use only within that method. They don't declare instance variables for an object. You can't use the keyword private on them, because that keyword is only for instance variables (and other things that aren't variables). You can't use the variables outside that method. If you declare them in an inner set of curly braces, you can't use them outside the curly braces. If you declare a local variable that's the same name as an instance variable, there's no connection between the two.
I am new to Java, I have below code, and getting exception like
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Calculator2Service
The method getCalculator2Port() is undefined for the type Calculator2Service
at com.theopentutorials.ws.calc2.client.Calc2Client.main(Calc2Client.java:13)
Please some one help..
package com.theopentutorials.ws.calc2.client;
import com.theopentutorials.ws.calc2.*;
public class Calc2Client {
/**
* #param args
*/
public static void main(String[] args) {
int a = 10;
int b = 12;
Calculator2Service calcService = new Calculator2Service();
Calculator2 calc = calcService.getCalculator2Port();
System.out.println(a + " + " + b + " = " + calc.add(a, b));
System.out.println(a + " - " + b + " = " + calc.sub(a, b));
}
}
You should define getCalculator2Port in the class Calculator2Service. If you're sure you've done this, please check the spells and note that Java is a case sensitive language.
BTW you may want to access getCalculator2Port while it's not visible in this scope, e.g. it's a private method, however in this case you get notified as "The method ... from the type ... is not visible".
Calculator2Port is a factory method which returns a Calculator2 object here. You should define an interface or abstract class like
public interface Calculator2 {
public double add(double a, double b);
public double sub(double a, double b);
}
then in Calculator2Service should have a method like
Calculator2 getCalculator2Port(){
Calculator2 c = new Calculator2(){
public double add(double a,double b){
return(a+b);
}
public double sub (double a, double b){
return(a-b);
}
}
return(c);
}
This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 9 years ago.
import java.util.Scanner;
import java.lang.Math;
public class Assignment1{
//instance variables
double a;
double b;
double c;
// method
public double hypot(double x, double y){
x = x*x;
y = y*y;
double z = x+y;
z = Math.sqrt(z);
return z;
}
// constructor
public Assignment1(double a , double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public static void main(String [] args){ // execution starts from here
Assignment1 obj = new Assignment1(a,b,c);
Scanner in = new Scanner(System.in);
System.out.println("Enter the values to compute the pythagoras theorem");
obj.a = in.nextDouble();
obj.b = in.nextDouble();
obj.c = hypot(a,b);
System.out.println("The hypot is"+ c);
}
}
What is the problem with this code... I m creating the objects... then it should worl fine !! Aint it???
In your main method (which is static) you are trying to access a b and c fields which are non-static without any reference to Assignment1 instance.
You need to understand that non-static members of class belongs to instance of class and they can be access only via such instance.
In non-static methods like
void someMethod(){
field = 42;
}
you can use field without reference to instance only because compiler will add this. to variable for you which will represent current instance. In static methods there is no this because static methods/fields belong to entire class, not to instances.
To solve your problem consider either first creating "unset" instance of Assignment1 class (lets say you set its values to 0) like
Assignment1 obj = new Assignment1(0, 0, 0);
then you can set its field to data from user like
obj.a = in.nextDouble();
obj.b = in.nextDouble();
and lastly you can calculate c value using
obj.c = obj.hypot(obj.a, obj.b);
BTW direct manipulation of object field is consider bad design. Instead you should create getters and setters (methods like getA() setA(int newA)) which will return or manipulate their values.
a, b and c are instance members. You main method is a class member.
You're trying to mix the two, which won't work.
Also, since you don't really need a custom constructor for your Assignment1 class, you can delete it.
Instead, try something like this:
public static void main(String [] args){ // execution starts from here
Assignment1 obj = new Assignment1();
Scanner in = new Scanner(System.in);
System.out.println("Enter the values to compute the pythagoras theorem");
obj.a = in.nextDouble();
obj.b = in.nextDouble();
obj.c = hypot(a,b);
System.out.println("The hypot is"+ c);
}
Of course its wrong,
you create a instance of Assignment1 with these Codes first:
Assignment1 obj = new Assignment1(a,b,c);
where are a and b and c??
maybe you should use null first:
Assignment1 obj = new Assignment1(null,null,null);
then use the Scanner.in to initialize a ,b and c
You should make a new class for Assignment. Having the main method here is a bit confusing. That said, the problem is that the first line of your main method:
Assignment1 obj = new Assignment1(a,b,c);
Accesses variables a, b, and c, which are non-static variables. This means they do not exist in the context of the static main function.
An alternative way to write this would be:
// Place this in a file called Assignment.java
import java.util.Scanner;
import java.lang.Math;
public class Assignment {
double a;
double b;
double c;
public double hypot(double x, double y) {
x = x*x;
y = y*y;
double z = x+y;
z = Math.sqrt(z);
return z;
}
public Assignment(double a , double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
} // end class Assignment
// Place this in a file called Main.java
public class Main {
public static void main(String [] args){ // execution starts from here
// Give some values to a, b, and c!
double a = 1;
double b = 1;
double c = 1;
Assignment obj = new Assignment(a,b,c);
Scanner in = new Scanner(System.in);
System.out.println("Enter the values to compute the pythagoras theorem");
obj.a = in.nextDouble();
obj.b = in.nextDouble();
obj.c = hypot(a,b);
System.out.println("The hypot is"+ c);
} // end method main
} // end class Main
As a general rule of thumb, try leave your main class empty of everything except the main method. It's even clearer if you call the class that contains the main method public class Main. Hope this helps!