Java multiple interface with parameter - java

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

Related

How to pass the value of variables from main class to another class?

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

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

Why wont my code run? Java program to add numbers

Can you help me find my error?
I'm trying to use these two methods here but my output is not working.
class Nine {
public static void Nine(String[] args) {
int x,y,z;
y = 3;
x = 7;
z = addEm(a, b);
System.out.println("answer= " +x);
}
public static addEm (double a, double b){
int c;
c = a+b;
}
}
Actually there are a lot of error in your code:
z=addEm(a, b);
here a and b are meaningless, you should use z=addEm(y,x); (if your intent is to sum three with seven)
System.out.println("answer= " +x);
I guess that you want to show the the results of the sum, therefore you should print z (and not x), so you should substitute with System.out.println("answer= " +z);
public static addEm (double a, double b) {
Here you missed the return type, and you need to consider also the type of parameters a and b. Since y,x and z are int, it is better if also a and b are int, and therefore specify also the return type as int:
public static int addEm (int a, int b) {
Or you can declare everything (y,x,z,a,b and return type) as a double: the important here is that they should be all of the same type. Moreover you miss also the return statement of the function addEm, that summarizing becomes:
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
And finally also the function
public static void Nine(String[] args)
it is not right named for an entry point: its names should be main.
So in conclusion, if you apply all the fix (by modifying as less as possible your original code) a code that compile, run and works following some 'logic' is:
class Nine {
public static void main(String[] args) {
int x, y, z;
y = 3;
x = 7;
z = addEm(y, x);
System.out.println("answer= " + z);
}
public static int addEm(int a, int b) {
int c;
c = a + b;
return (c);
}
}
Man, this is a very basic java lesson:
every prog need an entry point, which is in java:
public static void main(String args[]){}
And then your code will execute.
You're passing arguments a and b to addEm, but those variables aren't initialized. I'm expecting you wanted to pass x and y instead.
class Nine
{
public static void Nine(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " +x);
}
public static addEm (double a, double b)
{
int c;
c=a+b;
}
}
Your code will not work because your addEm method does not have any return type. In addition, the method you wrote takes Double params but while using you are trying to pass int to it. You also do not have any main method. I am assuming you misspelled or misunderstood the main method so below is the code which should work
class Nine
{
public static void Main(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " + x);
}
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
}

Cannot instantiate the type

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

Calling a method with parameters within another method located in a superclass

I was wondering how I could call getJD() within getToD and keep the parameters intact,(or temporarily set the parameters as variables in main and call the variables to the method).
The parameters are going to be input using the scanner class later in the main method.
import static java.lang.Math.*;
public class jdMethods
{
public static double getJD(double y, double m, double d){
if (m<=2.0){
y--;
m += 12.0;
}
double a=floor(y/100.0);
return (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}
public static double getToD(int h, int m, int s)
{
double a = getJD(a, a, a) + ((h-12)/24) + (m/1440) + (s/86400);
return a;
}
}
Edited for clarity.
It's not perfectly clear on what you are trying to do, but I assumed that you just want to save the result of your first getJD() and to use the result within your getToD(), so I made a private _jd and created a setter and getter for it.
import static java.lang.Math.*;
public class jdMethods
{
private double _jd;
public double getJD(){
return _jd;
}
public void setJD(double y, double m, double d){
if (m<=2.0){
y--;
m += 12.0;
}
double a=floor(y/100.0);
_jd = (365.25*(y+4716.0))+(30.6001*(m+1))+d+(2.0-a+floor(a/4.0))-1524.5;
}
public double getToD(int h, int m, int s)
{
double a = getJD() + ((h-12)/24) + (m/1440) + (s/86400);
return a;
}
}
So here is how you call it:
jdMethods testRun = new jdMethods();
testRun.setJD(1,2,3);
System.out.println(testRun.getToD(3, 2, 1));
All those parameters will be intact since you are using double and int, those are not Object s so it's value is copied when passed to a function, unlike Object s that a reference to it is passed to the function.
About your code, undefined variable a won't let it compile:
double a = getJD( a, a, a ) + ((h-12)/24) + (m/1440) + (s/86400);
I don't get what you are trying to do there, remember that a from getJD method is not the same a into getToD.

Categories

Resources