Cannot instantiate the type - java

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);
}

Related

Java multiple interface with parameter

Suppose i have 2 interface which is
interface add{
void add2No(float a, float b);
}
and
interface Minus{
void Minus2No(float a, float b);
}
then on the main method, i already overide the method which is
public class Count implements Add, Minus {
public static void main(String[] args) throws IOException{
//declare var
float a, b;
String temp;
//create object
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Count obj = new Count();
//User Input
System.out.print("Enter your first Number : ");
temp = br.readLine();
a = Float.parseFloat(temp);
System.out.print("Enter your second Number : ");
temp = br.readLine();
b = Float.parseFloat(temp);
System.out.println("Value of " +a+ " + " +b+ " is : " +obj.Totaladd(float a, float b));
System.out.println("Value of " +a+ " + " +b+ " is : " +obj.TotalMinus(float a, float b));
}
#Override
public void Add2No(float a, float b) {
float TotalAdd = a + b;
}
#Override
public void Minus2No(float a, float b) {
float TotalMinus = a - b;
}
}
Am i using the correct implementation for interface? why there's error when i try to print out the TotalAdd and TotalMinus?
Yes. Because you don't return the results. Currently both methods are void. You could change that. Like,
interface Add {
float add2No(float a, float b);
}
interface Minus {
float minus2No(float a, float b);
}
And then
#Override
public float add2No(float a, float b) {
return a + b;
}
#Override
public float minus2No(float a, float b) {
return a - b;
}
There are three wrong places.
The first wrong place:
Because Java is case sensitive.
The name of your interface method is called add2No, but the name of
your implementation is called Add2No
The second wrong place:
There is a problem with your method parameter passing and the way of
calling. I did not see the Totaladd and Totaladd methods defined in
your Count object.
If you adjust the case, there should only be add2No and Minus2No
methods in Count object.
You need to adjust the name of one of them, and do not pass the type when passing parameters.
For example: obj.Totaladd(float a, float b) should be obj.Totaladd(a, b)
The third wrong place:
If you need to call the method to get the value for calculation, you must adjust the type of the method, it should not be void

Actual and formal parameters

I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.

Overloading method :Automatic data type conversion

The following code is correct with respect to method overloading.
public class class7A {
public static void main(String[] args) {
testing obj_1 = new testing();
int a=12,b=14,c=20;
obj_1.func1(a,b,c); //invokes the 3rd method in the testing class
}
}
class testing{
void func1(int a,int b){
System.out.println("The values of length and breadth entered for the box is "+a+" "+b);
}
void func1(int a){
System.out.println("We can only talk about length here folks which is "+a);
}
void func1(double a,double b,double c){ //This method is invoked
System.out.println("The value of length ,breadth and height is "+a+","+b+","+c+" respectively");
}
}
Now the explanation given for the fact that the 3rd method is invoked even when the parameters defined for the 3rd method are "double" is that java automatically converts double into int here.I also know java does any operation on the primitive type by first converting the types into int at the back end which holds true for bytes as well.
However when i change the parameters of the 3rd method to be of byte type instead of double,the code gives an error .Foe example the code below gives an error :
Why it is happening so ?
public class class7A {
public static void main(String[] args) {
testing obj_1 = new testing();
int a=12,b=14,c=20;
obj_1.func1(a,b,c);
}
}
class testing{
void func1(int a,int b){
System.out.println("The values of length and breadth entered for the box is "+a+" "+b);
}
void func1(int a){
System.out.println("We can only talk about length here folks which is "+a);
}
void func1(byte a,byte b,byte c){ //This gives error
System.out.println("The value of length ,breadth and height is "+a+","+b+","+c+" respectively");
You must cast the type of data int to byte when you pass as argument of the method.
example:
public class class7A {
public static void main(String[] args) {
testing obj_1 = new testing();
int a = 12, b = 14, c = 20;
obj_1.func1((byte) a, (byte) b, (byte) c);
}
}
class testing {
void func1(int a, int b) {
System.out.println("The values of length and breadth entered for the box is " + a + " " + b);
}
void func1(int a) {
System.out.println("We can only talk about length here folks which is " + a);
}
void func1(byte a, byte b, byte c) { // This gives error
System.out.println("The value of length ,breadth and height is " + a + "," + b + "," + c + " respectively");
}
}
and if you want to make another type of conversion you can check this post where it is explained more detailed how to convert from int to byte
https://stackoverflow.com/a/842900/7179674

Quadratic Formula solution issue

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);

Accessing the int value outside of the method

I have a method A in class Test, which generates a number a and b, heres the code for it:
public class Test
{
int a,b;
public void A()
{
a = currentMenu.getCurrentFocusedItem().getItemID();
b = currentMenu.getMenuID();
System.out.println("Inside A ()" + a + " &&" + b);
}
public void B()
{
System.out.println("Inside B ()" + a + " &&" + b);
}
}
Now, I want to acces the a and b int values, into another method B(), in the same
class file.
Need some pointer
If you are working with the same instance of the class Test, the value of a and b as set in method A() should still be visible in method B().
So, the below would work:
Test test = new Test();
test.A();
test.B();
However, the below wouldn't
new Test().A();
new Test().B();
On a side note, methods in Java should always begin with a lowercase letter and use camelcase.
If what you are trying to do is to get the current(and latest) value of a and b,
you could write 2 methods like
public int getA() {
return currentMenu.getCurrentFocusedItem().getItemID();
}
public int getB() {
return currentMenu.getMenuID();
}
and use these methods instead of calling A() to update the values of a,b and then accessing them again in method B.
you can get the a and b values in instance initialization block
public class TestClass {
int a,b;
{
a= 10;
b =45;
}
public void A() {
System.out.println("Inside A ()" + a + " &&" + b);
}
public void B() {
System.out.println("Inside B ()" + a + " &&" + b);
}
}
Using this method, you don't have to call your A() method for populating the values to be used in B()
You can try this too
public void A()
{
a = currentMenu.getCurrentFocusedItem().getItemID();
b = currentMenu.getMenuID();
System.out.println("Inside A ()" + a + " &&" + b);
}
public void B()
{
Test test=new Test();
test.A(); // assign values for a and b
System.out.println("Inside B ()" + a + " &&" + b);
}

Categories

Resources