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 5 years ago.
Improve this question
I want to make sum of two numbers. But I have problems to do that. I don't understand why my sum is always zero.
class A
public class A {
int a=3 ,b=4;
public static void main(String[] args) {
B obj= new B();
obj.prod();
}
}
CLASS B
public class B {
int a, b;
public void prod()
{
System.out.print(a+b);
}
}
Uninitialized primitive fields have a default value. In the case of int this value is 0.
To pass the values from class A to B you should implement a constructor which takes two arguments.
public class B {
int a, b;
public B(int a, int b)
{
this.a = a;
this.b = b;
}
public void prod()
{
System.out.print(a+b);
}
}
Then in your class A, call the constructor of B like this:
public class A {
int a=3 ,b=4;
public static void main(String[] args) {
B obj= new B(a, b);
obj.prod();
}
}
Another way is to give your method prod() the parameters:
public void prod(int a, int b)
{
System.out.print(a+b);
}
And then call it:
public class A {
int a=3 ,b=4;
public static void main(String[] args) {
B obj= new B();
obj.prod(a, b);
}
}
public class A {
int a=3 ,b=4;
public static void main(String[] args) {
B obj= new B();
obj.prod(a,b);
}
}
public class B {
public void prod(int a, int b)
{
System.out.print(a+b);
}
}
I think the below code will help you:
public class A {
public static void main(String[] args) {
// create object B, passing aa=3, bb=4 to B's constructor
B objectB = new B(3, 4);
// this will return result as "12"
int result = objectB.prod();
}
}
public class B {
// declare member variables
private final int a;
private final int b;
// B's constructor
public B (int aa, int bb) {
// set values passed in to member variables
this.a = aa;
this.b = bb;
}
public int prod() {
int result = this.a + this.b;
// print result
System.out.print(result + "");
// return result to caller
return result;
}
}
You are initializing the values in class A. These are not available in class B. Either pass in constructor of class B or pass to the method prod().
You need to modify your class as below :
public class A {
int a = 3, b = 4;
public static void main(String[] args) {
B obj = new B(a, b);
obj.prod();
}
}
public class B {
int a, b;
b(int a, int b) {
this.a = a;
this.b = b;
}
public void prod() {
System.out.print(a + b);
}
}
//OR
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
public class A {
int a = 3, b = 4;
public static void main(String[] args) {
B obj = new B();
obj.prod(a, b);
}
}
//CLASS B
public class B {
int a, b;
public void prod(int a, int b) {
System.out.print(a + b);
}
}
Related
Suppose i have classes A,B,C. C has a method longRunningMethod, which takes a long time to run and returns an int. Classes A and B both have C as a dependency and need to call method longRunningMethod:
public class A{
private C c;
public A(C c){
this.c = c;
}
public void method1(){
this.c.longRunningMethod();
}
}
public class B{
private C c;
public A(C c){
this.c = c;
}
public void method2(){
this.c.longRunningMethod();
}
}
public class C{
public int longRunningMethod(){
...
}
}
public class MyProgram{
public static void main(String[] args){
C c = new C();
A a = new A(c);
B b = new B(c);
a.method1();
b.method2()//avoid calling c.longRunningMethod();
}
}
What approaches can be taken to avoid calling longRunningMethod twice? Of course, the simple approach is to change the constructor argument of A and B to int and call longRunningMethod once in MyProgram. But then, it is not that obvious to what to pass to A and B (which ints are allowed?).
public class C{
private boolean wasCalled = false;
private int cachedValue;
public int longRunningMethod(){
if (!wasCalled) {
// do your long running job here and set result to cachedValue
wasCalled = true;
}
return cachedValue;
}
}
I have a few classes which represent arithmetic operations (Plus, Minus, Pow...) in Java which extend the same abstract class Operator but they differ in one method -calculate.
I'm trying to find a way to avoid the switch-case conditions in order to implement these classes in the right way using design patterns (new to design patterns), but how do I do it? Or is the switch-case statement is the right way to implement it?
here is the abstract class:
public abstract class Operator {
private int a, b;
public Operator(int a, int b){
this.a = a;
this.b = b;
}
public float calculate() {
// here I want to return the result depending on the operator. If Plus extends Operator then the returned value is this.a + this.b
}
public void print() {
System.out.println("This is the result : %f", this.calculate());
}
}
In this case there is no need for a design pattern but using polymorphism is enough. Change the Operator class like the following:
public abstract class Operator {
protected int a;
protected int b;
public Operator(int a, int b){
this.a = a;
this.b = b;
}
public abstract float calculate();
public void print() {
System.out.println("This is the result : " + this.calculate());
}
}
And then implement it with Minus and Plus classes:
public class Plus extends Operator{
public Plus(int a, int b) {
super(a, b);
}
#Override
public float calculate() {
return this.a + this.b;
}
}
public class Minus extends Operator{
public Minus(int a, int b) {
super(a, b);
}
#Override
public float calculate() {
return this.a - this.b;
}
}
This is the class Main for a test:
public class Main {
public static void main(String[] args) {
Operator minus = new Minus(5,2);
minus.print(); // prints: This is the result : 3.0
Operator plus = new Plus(5,2);
plus.print(); // prints: This is the result : 7.0
}
}
Please see below code:
Change your calculate method to:
public float calculate(){
// here I want to return the result depending on the operator. If Plus extends Operator then the returned value is this.a + this.b
if(this instanceof Plus ){
return a + (float)b;
}else if(this instanceof Minus ){
return a - (float)b;
}//...For Mul and Div you can code
return 0.0f;
}
and after that, you can change your print method to:
public void print() {
System.out.println(String.format("This is the result : %f", this.calculate()));
}
and after that, you can do call this from any class to get your result:
Operator operator = new Plus(2,5);
operator.print();
operator = new Minus(5,2);
operator.print();
Lastly output:
This is the result: 7.000000
This is the result: 3.000000
I know it is impossible to override a method in one class. But is there a way to use a non-static method as static? For example I have a method which is adding numbers. I want this method to be usefull with an object and also without it. Is it possible to do something like that without creating another method?
EDIT:
What I mean is, if I make a method static I will need it to take arguments, and if I create an object with variables already set it will be very uncomfortable to call function on my object with same arguments again.
public class Test {
private int a;
private int b;
private int c;
public Test(int a,int b,int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public static String count(int a1,int b1, int c1)
{
String solution;
solution = Integer.toString(a1+b1+c1);
return solution;
}
public static void main(String[] args) {
System.out.println(Test.count(1,2,3));
Test t1 = new Test(1,2,3);
t1.count();
}
}
I know the code is incorrect but i wanted to show what I want to do.
I want this method to be usefull with an object and also without it.
Is it possible to do something like that without creating another
method?
You will have to create another method, but you can make the non-static method call the static method, so that you do not duplicate the code and if you want to change the logic in the future you only need to do it in one place.
public class Test {
private int a;
private int b;
private int c;
public Test(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public String count() {
return count(a, b, c);
}
public static String count(int a1, int b1, int c1) {
String solution;
solution = Integer.toString(a1 + b1 + c1);
return solution;
}
public static void main(String[] args) {
System.out.println(Test.count(1, 2, 3));
Test t1 = new Test(1, 2, 3);
System.out.println(t1.count());
}
}
But is there a way to use a non-static method as static?
No, it's not possible.
If you need this method to be used in static and non-static context, then make it static. The opposite configuration, however, is not possible.
Make it static, then it can be used with object and without it.
public class MyTest() {
public static int add() {
System.out.println("hello");
}
}
MyTest.add(); //prints hello
MyTest myobject = new MyTest();
myobject.add(); //prints hello
I've been using multiple methods, but my "java the complete reference" book doesn't do a good job of explaining how to use the "this" keyword.
this in java
It is used to refer to the data members of the object in the envoked method or constructor in case there is a name conflict between fields and local variables
public class Test {
String s;
int i;
public Test(String s, int i){
this.s = s;
this.i = i;
} }
It is used to invoke one constructor from another constructor of the same class or you can say constructor chaining.
public class ConstructorChainingEg{
String s;
int i;
public ConstructorChainingEg(String s, int i){
this.s = s;
this.i = i;
System.out.println(s+" "+i);
}
public ConstructorChainingEg(){
this("abc",3); // from here call goes to parameterized constructor
}
public static void main(String[] args) {
ConstructorChainingEg m = new ConstructorChainingEg();
// call goes to default constructor
}
}
It also facilitates method chaining
class Swapper{
int a,b;
public Swapper(int a,int b){
this.a=a;
this.b=b;
}
public Swapper swap() {
int c=this.a;
this.a=this.b;
this.b=c;
return this;
}
public static void main(String aa[]){
new Swapper(4,5).swap(); //method chaining
}
}
Here's a couple:
public class Example {
private int a;
private int b;
// use it to differentiate between local and class variables
public Example(int a, int b) {
this.a = a;
this.b = b;
}
// use it to chain constructors
public Example() {
this(0, 0);
}
// revised answer:
public int getA() {
return this.a;
}
public int getB() {
return this.b
}
public int setA(int a) {
this.a = a
}
public void setB(int b) {
this.b = b;
}
}
this refers to the attributes that belong to the object in which this is used in. For example:
Example ex1 = new Example(3,4);
Example ex2 = new Example(8,1);
In these cases, ex1.getA() will return 3, because this is referring to the a that belongs to the object named ex1, and not ex2 or anything else. The same goes for ex2.getB().
If you look at the setA() and setB() methods, using this distinguishes the attributes a and b belonging to the object from the parameter names as they're the same.
I am trying to add a parameter at the declaration of a class.
Here is the declaration:
public static class TCP_Ping implements Runnable {
public void run() {
}
}
This is what I am trying to do:
public static class TCP_Ping(int a, String b) implements Runnable {
public void run() {
}
}
(which doesn't work)
Any suggestions? thanks!
You probably want to declare fields, and get the values of the parameters in the constructor, and save the parameters to the fields:
public static class TCP_Ping implements Runnable {
// these are the fields:
private final int a;
private final String b;
// this is the constructor, that takes parameters
public TCP_Ping(final int a, final String b) {
// here you save the parameters to the fields
this.a = a;
this.b = b;
}
// and here (or in any other method you create) you can use the fields:
#Override public void run() {
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
Then you can create an instance of your class like this:
TCP_Ping ping = new TCP_Ping(5, "www.google.com");
Use Scala! This is supported nicely.
class TCP_Ping(a: Int, b: String) extends Runnable {
...
You cannot declare concrete parameters on the class heading (there are such things as type parameters, but that's not what you need as it appears). You should declare your parameters in the class constructor then:
private int a;
private String b;
public TCP_Ping(int a, String b) {
this.a = a;
this.b = b;
}