instance variable in another class - java

I'm working on homework and I won't post the full code but I'm stuck on something that's probably simple and I can't find it in my book so I need to be pointed in the right direction.
I'm working with classes and interfaces.
Basically in my main code I have a line like this
CheckingAccount checking = new CheckingAccount(1.0); // $1 monthly fee
I was told to create a class called CheckingAccount and in that class I am told "This class should include an instance variable for the monthly fee that's initialized to the value that's passed to the constructor.
Since I'm new this is barely english to me and I'm assuming what that is saying is to take that 1.00 fee and declare it in the CheckingAccount class so I can create a method using that variable to calculate something.
soooo... How do I do that? I know how to create an instance variable it would be something like
public double monthly fee =
but then what? or I could be wrong. I am really doing bad at this java stuff. Any help is appreciated.
I guess another way to ask it is am I just declaring it as 1.0? or am I "importing" that value in case it changes later at some point you don't have to go through the code to change it in all of the classes?

Your requirement (as I read it) is to initialize the instance variable in the constructor, and your instantiation (new CheckingAccount(1.0);) shows you are on the right track.
What your class will need is a constructor method which receives and sets that value 1.0.
// Instance var declaration
private double monthly_fee;
// Constructor receives a double as its only param and sets the member variable
public CheckingAccount(double initial_monthly_fee) {
monthly_fee = inital_monthly_fee;
}

#Jeremy:
You're pretty much spot on (at least, your interpretation of what you've been asked to do matches my interpretation); while I don't know the actual design of the class, or whether monthly_fee needs to be public, in pseudocode you'd be looking at something like:
class CheckingAccount {
//Instance variable
double monthly_fee;
//Constructor
CheckingAccount(double monthly_fee) {
this.monthly_fee = monthly_fee;
}
//Function to multiply instance variable by some multiplier
//Arguments: Value to multiply the monthly fee by
double multiply_fee(double a_multiplier) {
return monthly_fee*a_multiplier;
}
}

You are basically right. If you haven't already, you should create a new class (it should be in it's own file called CheckingAccount) like this:
/** This is the class of Account with monthly fee. */
public class CheckingAccount {
// This is the instance variable.
// It should be 'private' for reasons you will surely learn soon.
// And NOT static, since that would be a class variable, not an instance one.
// The capitalization is called camelCase, google it up. Or, even better, find 'JavaBeans naming conventions'
private double monthlyFee;
// This is the constructor. It is called when you create the account.
// It takes one parameter, the fee, which initializes our instance variable.
// Keyword 'this' means 'this instance, this object'.
public CheckingAccount(double monthlyFee) {
this.monthlyFee = monthlyFee;
}
// Here will be your methods to calculate something...
}

Don't create an instance variable as public. It's bad practice because it violates the principle of information hiding (your teacher may call this abstraction). Instead, you can create an instance variable as
public final class CheckingAccount {
private double monthlyFee;
// The rest of the class goes here
public double getMonthlyFee() { // This method is called an accessor for monthlyFee
return monthlyFee;
}
}
Note that monthly fee isn't a valid variable name because it contains a space, and variable names can't contain spaces. Also notice that other classes access monthlyFee through a method. Because you define the method rather than making the variable public, you control access to monthlyFee a lot better (another class can't just change monthlyFee unless you define a method that makes that change).
Now to accessing monthlyFee. The method getMonthlyFee is called an accessor for a reason: it allows other classes to access that variable. So, those other classes can just call the method to get the monthly fee out of a CheckingAccount:
CheckingAccount checking = new CheckingAccount(1.0);
// A bunch of other code can go here
double fee = checking.getMonthlyFee(); // Now fee is 1.0

Related

Using 'this' keyword in a class with a no arg (default constructor) in Java?

I have a quick question. I know how to use the keyword this in Java with a constructor that has parameters/arguments. Can you use this with a default constructor that has no parameters/arguments?
Example with code Class BankAccount below.
We created a method within this class to withdraw as much as possible. Within the method, I created a new BankAccount object to test with the tests provided by the Professor. Instead of creating theaccount object, he wanted me to use this. Is this possible without a constructor that holds parameters/arguments?
public double getOrAsMuchAsPossible(double requestAmount) throws InvalidAmountException, InsufficientFundsException
{
//Declare and initialize the variable amount to be used with in the method
double amount = 0;
//Create a new BankAccount object account
BankAccount account = new BankAccount();
//Deposit money into the account
account.deposit(400);
//Try to get requestAmount
try
{
//Set the amount to the request amount and withdraw from account
amount = requestAmount;
account.withdraw(requestAmount);
}
//Catch the exception with the InsufficientFundsException
catch(InsufficientFundsException exception)
{
System.out.println("Withdrawing amount: " + amount + " that is larger than balance: " + balance + " is not allowed");
}
//If the account balance is less than the amount requested
if(account.balance<requestAmount)
{
//The amount will equal the account balance, withdraw the amount from the account
amount = account.getBalance();
account.withdraw(amount);
}
return amount;
}
The java keyword "this" has no special interaction with constructors. It is often used in constructors to distinguish between parameter names and the newly created object's fields.
Something like
public class BankAccount {
private int accountNum;
public BankAccount() {
this.accountNum = 4;
}
}
Is perfectly valid, but redundant.
The main value to the "this" keyword in java is to access a field in a higher scope that has been masked in the current scope.
Classic Setter example
public void setAccountNum(int accountNum) {
this.accountNum = accountNum;
}
In this case all references to accountNum would refer to the parameter. Use of the "this" keyword allows us to specify that it is the object's field called accountNum that is to be assigned a value.
Firstly, what is "this" keyword in java?
From oracle java docs:
Using the this Keyword within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
So coming to your question, your constructor need not to be parameterized to use this. You can use this keyword with default constructor as well.
Basically you only need to remember "The this keyword refers to the current object in a method or constructor."

Instance variables default value

My book says that one of the crucial differences between local variables and instance variables are that we must initialize local variables
and instance variables get a default value of 0 if you don't initialize them. Shortly before this I did an example introducing constructors, and what public void and public double and
return do.
Consider the following example which I just did in my book. Here balance is an instance variable.We have two constructors below it. If an instance variable gets a default value of zero, why do I need the first constructor
public Account(){
balance = 0;
}
saying that if I call something like Account acc = new Account(); balance = 0. Doesn't it get to zero automatically? At least thats my understanding from my book
Heres the full code i was working on
public class Account {
private double balance;
public Account(){
balance = 0;
}
public Account(double initialBalance){
balance = initialBalance;
}
public void deposit(double amount){
balance = balance+amount;
}
public void withdraw(double amount){
balance = balance-amount;
}
public double getBalance(){
return balance;
}
}
public class Test {
public static void main(String [] args){
Account acc = new Account(500);
acc.deposit(500);
System.out.println(acc.getBalance());
}
}
You don't need the first constructor as you never call it anywhere.
Assuming it was called somewhere, you wouldn't need the line balance = 0 in it, but some people would still add it, just to make it explicitly visible and clear that it's intentional (sometimes automatic things are used unintentionally).
If you removed the constructor entirely and tried to instantiate an Account using just new Account() (without any parameters), then that wouldn't work as you don't have a zero-argument constructor anymore (another magic thing: Java will generate one for you if and only if you don't have any other constructors).
Yes, it does have 0.0 as a default value.
All primitive types (int, double, ...) will be initialized to 0 and all reference to other types (somehow extending Object) will be initialized to null.
It is good practise to (re)initialize the members to 0 (or any other value that make sense for the class), to show you didn't forget the variable. When you later add new members, if its value is not initialized in a constructor, it can be a hint that you forgot about it.
You can also decide to initialize it to any value during declaration:
private double balance = 0.0;
and do the same for all other members. In that case, they would receive that value when the constructor is not assigning any specific. That pattern could be used to show your intent of having those values as default, and the ones set by a constructor as an "override".
Yes, all primitive types (int, double, long...) are automaticly initialized with 0. Like my previous speaker said it is a good practise to add the line, to makes things clearer and cleaner.
But think also about complex types (String, Integer, Double, or your own Class). They will be initialized with null. If you try to access this default value you will get a NullPointerException, so a existing default value would be better.
A constructor is used to initialize the instance variables.The default value is 0(after initialization in case of int). However,in your code you are not even calling the default constructor.So,in your code you can remove the default constructor.
Moreover,even if you call the default constructor ,the values will be initialized automatically.You don't even have to mention balance=0 in your code.Only this would be enough:
public account(){}
However,if you don't call any constructor and try to access the instance variables through a reference variable you will get the same error you get in case of local variable -
The variable might not have been initialised
You can learn better from here https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

When to use a local access modifier? [duplicate]

This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 6 years ago.
While working in intelliJ I was recommended to make a method be localized - by removing the access modifier. I realized I don't really know which modifier to use and when and would like some clarification.
I read this post: What is the difference between Public, Private, Protected, and Nothing?
and as that's for C#, although they're similar, I wanted to make sure it's not too different.
I also read the Javadoc post here: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
and was still a bit skeptical.
Thanks in advanced!
Access modifiers help with the OOP principle of encapsulation. If you create a well-written class anyone will be able to use it without knowing all the internal details of how it works. When you define a method as public any code that uses your class will have access to that method. You only want to expose methods the end user of your class will need. If it's a helper method that's does some internal calculation relevant only inside your class, declare it as private. It will cut down on the methods available to the end user and make your class much easier to use. Here's an example:
Public Class Car {
private int speed;
private float fuelNeeded;
private float afRatio = 14.7;
public void speedUp { speed++; }
public void speedDown { speed--; }
public float getFuelNeeded {
calculateFuelNeeded(speed);
return fuelNeeded;
}
private void calculateFuelNeeded (int speed) {
// Do some complex calculation
fuelNeeded = someValue / afRatio;
}
}
Anyone who creates a Car object will have three methods available, speedUp, speedDown, and getFuelNeeded. calculateFuelNeeded is only used internally by the class, so there's no reason for the user to see it. This way they don't need to know how the amount is calculated, or when the method needs to be called, or what values it sets internally. They just get the value easily with getFuelNeeded();
When declaring variables, it is usually always correct to declare them private, having a public get() and set() method for any that need to be accessed from outside the class. The main reason for this is to prevent an outside user from setting the value something invalid. In our Car class, afRatio needs to be nonzero because we are using it as a divisor. If we declare it as public, a user could easily write this code:
Car car = new Car();
car.afRatio = 0;
someFloat = car.getFuelNeeded();
Of course this would cause our class to throw an ArithmeticException for divison by zero. However, if we did this:
private afRatio = 14.7;
public void setAfRatio(float ratio) {
if(ratio <= 0)
throw new IllegalArgumentException("A/F ratio must be greater than zero");
afRatio = ratio;
}
This allows us to check user given parameters and ensure our variables don't get set to some invalid value.

Java - Cannot Access Abstract Method from Subclass Object

I'm working on a Java project where I have a superclass called Personnel, and two subclasses; Manager and Worker.
The subclasses contain separate variables that are used to hold information about their pay, and I previously had method getSalary() for Manager and getWage() for Worker. However, I wanted a number of these to be accessible through a for-loop so I put an abstract method into the Personnel class called getAnnualIncome() and put a corresponding method into both subclasses:
public abstract class Personnel
{
//attributes of each instance of class
protected String name;
protected int payrollNum;
protected String dept;
//abstract method to be defined in subclasses
public abstract float getAnnualIncome();
}
In Manager:
public float getAnnualIncome()
{
//convert salary variable to real number
float salaryAsFloat = salary;
//return real number
return salaryAsFloat;
}
In Worker:
public float getAnnualIncome()
{
//number of weeks in a year as constant value
final int weeksInYear = 52;
//weekly wage by number of weeks in a year
return ((hourlyRate * hoursPerWeek) * weeksInYear);
}
This all works fine in theory, but when I came to actually implementing the for-loop I mentioned (using an array of the Personnel class with objects from both subclasses as elements), the following piece of code did not recognize the getAnnualIncome() method (this is taken from the executable main class):
for (int index = 0; index < employee.length; ++index)
{
System.out.printf(employee[index].getName()
+ "\t\t" + "£%.2f %n", employee[index].getAnnualIncome());
//error message appears here ^^ 'method is undefined in 'Personnel'
}
Not really sure what's going wrong here, thought I'd covered everything that needed to be done. As you can see from my code extracts, the method is in the superclass!
I don't really know where to go from here, any shove in the right direction would be greatly appreciated!
Thanks,
Mark
Are you able to create an array of the Personnel class with objects from both subclasses as elements?
because that itself will give you error. "Can not convert subclass(Worker/ Manager) to Personnel[].
In case that works fine for you, than the problem is with the annotation #Override.
When you made an abstract class method, you are telling the JVM, "Childs" from this "Parent" WILL have those methods, and will either perform them, or have their "Childs" perform them.
Absctract class methods cannot use Objects details in themselfs, they are not able to be instantiated.
Either have getAnnualIncome() in both Worker and Manager (and their "Children") or have that abstract class do the method.
To fix your issue, use the annotation #Override, to inform Java 6+ that the code is legitimated (while optional, this could help), and check that the hierarchy is correct

what is the extra benefit of creating constructor in java?

I have noticed a thing that a constructor and a simple method of a class do the same work. what is the exact reason to create a construct of a class? If i create MyClass(){} constructor and MyClassMethod(){} method it will do the same work as I write the body part of those method and constructor. So what is the need of construct? Does it have any special use ?
A constructor and a method are two different things. The fact that you can write the same or similar code inside them is irrelevant.
When a new object is created a constructor is called. If you don't specify one the compiler will create a default one for you. This is the place where initializaton of the object's fields takes place and memory is allocated for the object. This is a concept that all object-oriented languages have. A new object must be initialized somehow. Memory needs to be allocated. In Java you don't manage the memory yourself (at least not directly anyway) so this is the purpose of the constructor. Note that since a constructor is always executed, this behaviour is enforced as soon as you call e.g. Person p = new Person();.
Now since a constructor is always being called, you have an option here: do you let the default constructor execute or do you create one yourself? Perhaps there are fields that need to be initialized in a way other than their default values. Or perhaps you need to not allow creating an object without providing some values. If you define a constructor yourself, the compiler does not create a default one for you. So if I have public Person(String firstName, String lastName) {} then I have created a specific rule that is again enforced by the system: a new object of class Person cannot be created unless you give a first name and last name:
Person p = new Person(); // this would give a compile error
Person p = new Person("John", "Smith"); // this is the only way to create an object now
Using a method you cannot enforce this. The programmer using your class might call your method or not. The constructor is a part of the lifecycle of the object. Methods define the behaviour of the object
Some points :
1) Constructors are the only way to set final instance variables .
public class SomeType {
final int x ;
SomeType(int y){
x=y;
}
}
2) A class with private constructor cannot be sub classed.
3) If your class is a subclass and the base class doesn't have a default constructor , then you need a constructor in your class to call the super class constructor.
One of the benefits of using a constructor over a method is that you can be assured the constructor was called and the work within the constructor was performed.
The language specifies that to construct an object a constructor must be called. So if you use a custom method to establish the initial state of your object, you will need to call the default constructor first. Why make two method calls when you can perform the work in one call the constructor and be assured the object has been properly initialized?
public class Test(){
private Integer x;
public Test(){
}
public Test(Integer x){
this.x = x;
}
public void setX(Integer x){
this.x = x;
}
public void doSomethingWithX(){
this.x.toString();
}
}
Test test = new Test(8);
test.doSomethingWithX(); //I know x has been declared and assigned
Test test = new Test();
test.doSomethingWithX(); //X has not been assigned results in NPE
If you create a new Object of MyClass it will automatically call the constructor - you can initialize all members within it, and be sure that this object´s members are all initialized.
Generally:
A constructor is always called once when you create a new Object of this class, and you can´t call it manually.
And don´t do "real" work in a constructor, as it will slow down the creation of objects of this class - only initialize your class/members there.
You can also use different constructors, depending on your needs - but if you create a constructor, there is no more default constructor!
Example:
public MyClass {
int score;
public MyClass(int sc) { // already know the score
score = sc;
}
public MyClass() { // don´t know the score yet
score = 1;
}
public void addScore() {
score += 5; // i know for sure that score is not zero
}
}
Essentially a constructor is just a special method that implicitly returns an object of its containing type. You should generally use constructors for creating objects - this is what people expect to see.
However, there is a useful idiom called the factory method (more info at this link) which is essentially using a static method to construct an object, the key advantages being
You can give a factory method a more descriptive name (whereas of course a standard constructor has to be named after the containing class).
They don't have to return an object, giving more flexibility.
They can return a sub-types of the class.
You can set final fields without initializer in a constructor. This helps to build immutable instances:
class Number extends Expr {
private final int n;
public Number(int n) {
this.n = n;
}
public int getValue() {
return this.n;
}
}
So after a constructor like this, you can rely on the fact that the instance is initialized completely (and in this case, it's values are immutable/constant).
Constructor is not like simple methods. It is called every time when the object of that particular class is created. You don't need to call it explicitly.
There are somethings that we need to do immediately when the object is created, for instance when you create a GUI kind of thing you want to set many properties on the time of creation like size of window etc.
Another benefit of constructor is security of class. You cannot create a object unless you know the right perimeters of constructor.
More details:http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type.
Some points :
1. A constructor eliminates placing the default values.
2. A constructor eliminates calling the normal method implicitly.
These are the benefits of constructors.
Automatic initialization of objects at the time of their declaration.
Multiple ways to initialize objects according to the number of
arguments passes while declaration.
The objects of child class can be initialised by the constructors of base class.

Categories