Is there any error in the following Java code? I did not find one when I ran it.
public class WayToGo
{
private int aa, bb;
public void WayToGo(int a, int b)
{
aa = a;
bb = b;
}
}
If this is supposed to be a constructor :
public void WayToGo( int a,int b)
change it to :
public WayToGo( int a,int b)
A constructor has no return type. Not even void.
public class WayToGo {
private int aa, bb;
public WayToGo(int a, int b) {
***this.aa*** = a;
***this.bb*** = b;
}
}
Related
So i know how to use it, how it works.The question what is the point in real life scenario.
Imagine created class without toString() overriding. So what is the point in that class if you can't display it properly ??
And please try not to explain how constructor chaining works or something like that.
I know how it works. I want to know does anyone do this in real life
because without toString() overriding i don't see the point
public class ConstructorChaining {
String a;
int b;
int c;
int d;
int e;
public ConstructorChaining() {
this("");
}
public ConstructorChaining(String a) {
this(a, 0);
}
public ConstructorChaining(String a, int b) {
this(a, b, 0);
}
public ConstructorChaining(String a, int b, int c) {
this(a, b, c, 0);
}
public ConstructorChaining(String a, int b, int c, int d) {
this(a, b, c, d, 0);
}
public ConstructorChaining(String a, int b, int c, int d, int e) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
}
so imagine i created an object
ConstructorChaining constructorChaining=new ConstructorChaining("name");
and tried to print it
System.out.println(constructorChaining);
How do i implement toString() for this
Just do this, using a field that is set differently based on the constructor you called:
public class ConstructorChaining {
String a;
int b;
//This value is different for each constructor, so you can control your
//toString implementation
String asString;
public ConstructorChaining() {
this("");
}
public ConstructorChaining(String a) {
this(a, 0, a + "");
}
public ConstructorChaining(String a, int b) {
this(a, b, 0, a + "" + b);
}
private ConstructorChaining(String a, int b, String asString) {
this.a = a;
this.b = b;
this.asString = asString;
}
#Override
public String toString() {
return "Overriden toString, asString = " + asString;
}
public class ConstructorChaining {
String a;
int b;
int c;
int d;
int e;
public ConstructorChaining() {
this("");
}
public ConstructorChaining(String a) {
this(a, 0);
}
public ConstructorChaining(String a, int b) {
this(a, b, 0);
}
public ConstructorChaining(String a, int b, int c) {
this(a, b, c, 0);
}
public ConstructorChaining(String a, int b, int c, int d) {
this(a, b, c, d, 0);
}
public ConstructorChaining(String a, int b, int c, int d, int e) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
#Override
public String toString() {
return "ConstructorChaining={a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + ", e=" + e;
}
}
Look what your friends are telling you, there is no relation between the contractors and the toString overridden method.
All the primitive types are already initialized as 0 so you don't need to care if the values are there or not.
I was told by my lecturer that i have no accessor and mutator method in my class,but i have no idea what he means as i did include my accessor and mutator methods.
The 2 issue i can come up with are:
1.My mutator have to be for each individual variable instead of all the variable at once.
2.My sub-class needs accessor and mutator method for my super class variables.
I did ask my lecture but he said go figure it out yourself, and i didn't include the toString
abstract class TwoD implements Shape
{
//protected instance variables as the subclasses will use them
protected int a;
protected int b;
protected int c;
//default constructor
public TwoD() {}
//constructor for circle
public TwoD(int a)
{
this.a = a;
}
//constructor for rectangle
public TwoD(int a, int b)
{
this.a = a;
this.b = b;
}
//constructor for triangle
public TwoD(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
//copy constructor
public TwoD(TwoD td)
{
this (td.a, td.b, td.c);
}
//accessor methods to get variables
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public int getC()
{
return c;
}
//mutator methods to set variables
public void setA(int a)
{
this.a = a;
}
public void setAB(int a, int b)
{
this.a = a;
this.b = b;
}
public void setABC(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
class Circle extends TwoD
{
//default constructor
public Circle() {}
public Circle(int radius)
{
super(radius);
}
//method to calculate area of circle
#Override
public double area()
{
return Math.PI * a * a;
}
//method to get calculated area
#Override
public double getArea()
{
return area();
}
Accessor methods are often called getters and mutator methods are often called setters.
A widely used pattern within the Java world is that you
make your fields (instance variables) private
private int a;
add a getter if you need an accessor method
public int getA() {
return this.a;
}
add a setter if you need a mutator method
public void setA(int a) {
this.a = a;
}
Accessor and mutator methods almost always change a single field.
Note that I, just like Aaron Davis, don't like this design either. Since subclasses are only able to add functionality, and are unable to remove or hide it, one must choose wisely which class extends the other. An example would be the well-known squares-rectangles problem.
You also need to use self-descriptive names. a, b and c should be renamed to something better describing what those variables represent.
I have some final fields in the class like
class A {
private final boolean a;
private final boolean b;
public A(boolean a){
this.a = a;
}
public A(boolean a, boolean b){
this.a = a;
this.b = b;
}
}
But this gives an error that final field 'b' might not have been initialized.
So any help would be appreciated on how to handle final attributes initialization in case of multiple constructors. It works fine if I have only the second constructor.
You can initialize b to default false. All the final variable should be initialized in constructors.
public A(boolean a){
this.a = a;
this.b = false;
}
Or should call other constructors which would initialize them.
public A(boolean a){
this(a, false);
}
public A(boolean a, boolean b){
this.a = a;
this.b = b;
}
the problem is that first constructor does not initialize b, so java cannot assume any value, standard practice is to write code like this:
public A(boolean a){
this(a, DEFAULT VALUE FOR B);
}
public A(boolean a, boolean b){
this.a = a;
this.b = b;
}
this way you have only 1 real constructor, all other constructors are just short-cuts for it
you can call the constructor from another constructor too:
public class A{
private final boolean a;
private final boolean b;
public A(boolean a){
this(a,false);
}
public A(boolean a, boolean b){
this.a = a;
this.b = b;
}
}
Here's my question, how can I change an object outside of it's class, so that it maintains the changes made in the outside class?
Here's an example of the code:
Main class:
public class Main {
public static void main(String[] args)
{
Variable var = new Variable(1,2,3);
Change.changeVar(var);
System.out.println("" + var.geta() + "" + var.getb() + "" + var.getc());
}
}
Variable class:
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
}
Change class:
public class Change {
public static void changeVar(Variable var)
{
Variable var2 = new Variable(4,5,6);
var = var2;
}
}
In your example, no. When changeVar() exits, the parameter var is discarded, and the var in your main() method retains its original value. Read up on pass by reference.
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
// depending on your use case, setters might be more appropriate
// it depends on how you want to control the changing of the vars
public void update(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
public class Change {
public static void changeVar(Variable var)
{
var.update(4,5,6);
}
}
You cannot do it in a way that you described, because in Java variables are passed by values. However you can achieve the desired effect in a different way:
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
public void seta(int a) { this.a = a; }
public void setb(int b) { this.a = b; }
public void setc(int c) { this.a = c; }
}
public class Change {
public static void changeVar(Variable var)
{
var.seta(4);
var.setb(5);
var.setc(6);
}
}
You need to provide setter methods and call them on the original object:
public void seta(int newa) { this.a = newa; }
Then you would say
public static void changeVar(Variable var)
{
var.seta(4);
//etc
}
You are merely repointing the local variable reference var to point to your new instance var2. It has no effect on the value of the original instance passed into the method.
Doing it that way? You can't.
You're passing a reference to the instance. However, inside the function, you use a new reference. Assigning to the new reference does not affect others.
public static void changeVar(Variable var)
{
Variable var2 = new Variable(4,5,6);
var = var2;
}
first, u can write some setter methods in Variable class, then you can call these setter methods in the above code, like var.setA(4) ... and so on.enter code here
I got an implementation of Parcelable working for a single class that involves no inheritance. I have problems figuring out the best way to implement the interface when it come to inheritance. Let's say I got this :
public abstract class A {
private int a;
protected A(int a) { this.a = a; }
}
public class B extends A {
private int b;
public B(int a, int b) { super(a); this.b = b; }
}
Question is, which is the recommended way to implement the Parcelable interface for B (in A? in both of them? How?)
Here is my best solution, I would be happy to hear from somebody that had a thought about it.
public abstract class A implements Parcelable {
private int a;
protected A(int a) {
this.a = a;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(a);
}
protected A(Parcel in) {
a = in.readInt();
}
}
public class B extends A {
private int b;
public B(int a, int b) {
super(a);
this.b = b;
}
public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(b);
}
private B(Parcel in) {
super(in);
b = in.readInt();
}
}
This is my variant. I think it's nice because it shows the symmetry between the virtual read- and write- methods very clearly.
Side note: I think Google did a really poor job at designing the Parcelable interface.
public abstract class A implements Parcelable {
private int a;
protected A(int a) {
this.a = a;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(a);
}
public void readFromParcel(Parcel in) {
a = in.readInt();
}
}
public class B extends A {
private int b;
public B(int a, int b) {
super(a);
this.b = b;
}
public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(b);
}
public void readFromParcel(Parcel in) {
super(in);
b = in.readInt();
}
}
Here is the implementation for class A in a real world setting since class B will likely have more than one object with different types other than int
It uses reflection to get the types. Then uses a sorting function to sort the fields so that reading and writing happen in the same order.
https://github.com/awadalaa/Android-Global-Parcelable