#How does inheritance work

1 messages · Page 1 of 1 (latest)

tardy bay
#

HELP. IDK HOW TO DO THIS NEXT PART?
"The account class was
defined to model a bank account. An account has the properties account number,
balance, annual interest rate, and date created, and methods to deposit and withdraw funds. Create two subclasses for checking and saving accounts. A checking
account has an overdraft limit, but a savings account cannot be overdrawn."

import java.util.Date;
import java.util.Scanner;

public class Account {
    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private final Date dateCreated = new Date();

    public Account() {
    }

    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public double getMonthlyInterestRate() {
        return annualInterestRate/12;
    }

    public double getMonthlyInterest() {
        return getMonthlyInterestRate() * balance;
    }

   public void withdraw(double withDrawAmount) {
       System.out.println("How much would you like to withdraw?");
       balance -= withDrawAmount;
       System.out.println("You withdrew " + withDrawAmount + " and your balance is now " + balance);
   }
    public void deposit(double depositAmount) {
        System.out.println("How much would you like to deposit?");
        balance += depositAmount;
        System.out.println("You deposited " + depositAmount + " and your balance is now " + balance);
    }

}
topaz citrusBOT
#

<@&987246399047479336> please have a look, thanks.

uncut vapor
quaint kiln
#

Also what specifically is unclear?

uncut vapor
#

you need to define a new class, use extends to extend an existing class

tardy bay
tardy bay
uncut vapor
quaint kiln
#

Dioxin gave a (too) strong nudge. What have you learned during your class in relation to inheritance?

tardy bay
#

i guess i never understood it

#

i looked at my class to see maybe he will have slides on inheritance but he doesnt

#

can someone pls help me??

#

this lab is due tomorrow I dont know what im doing

quaint kiln
#

I recommend checking out:

topaz citrusBOT
#

For learning Java, we recommend MOOC.

It is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

Visit MOOC here:
https://java-programming.mooc.fi
(the course is available in both English and Finnish)

  • The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
    The end of the course is marked by creating your own Asteroids game clone!
  • The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
  • To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.

The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ, which we generally recommend.

  • TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
  • VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
  • IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
    IntelliJ requires installing a version no newer than 2023.1 - because the IntelliJ TMC Plugin doesn't work with newer installs.
    The IntelliJ Community version is completely free and all you need to install the TMC plugin.

To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

About the course - Java Programming

quaint kiln
#

We can answer some questions, but we can't do your assignment for you.

tardy bay
quaint kiln
#

Your exact scenario is in there.

uncut vapor
tardy bay
tardy bay
uncut vapor
quaint kiln
#

I am not logged in, and it's even defined in the outline.

tardy bay
quaint kiln
#

You can click on the chapters to see the outline.

topaz citrusBOT
#

How does inheritance work

#

Changed the title to How does inheritance work.

tardy bay
#

I looked at intro to OOP

#

and objects on a list

quaint kiln
#

Ok, so I renamed your topic.

#

And both Dioxin and I gave what you're trying to a name.

#

When looking at the parts, is there a topic that mentions this notion/seems related to it?

tardy bay
quaint kiln
#

Ok, mind listing me the topics of chapter 9?

tardy bay
#

sorry i was loking at parts 1-7

quaint kiln
#

No worries, it's just a good idea to first look at the full overview of a course. That way you can easily find a topic when you get stuck.

quaint kiln
#

Which aspect is unclear?

tardy bay
# quaint kiln Which aspect is unclear?

my friend showed me their answer and its literally so confusing.
public class Savings extends Account{
private boolean overdraft = false;

public Savings() {
    super();
}

public boolean isOverdraft() {
    return overdraft;
}

public void setOverdraft(boolean overdraft) {
    this.overdraft = overdraft;
}

}

#

I dont understand what anything is doing

#

Or how to write it myself

quaint kiln
#

That answer seems wrong to me, but what's unclear about it?

#

I think your challenges go further than inheritance and you should refresh the fundamentals.

uncut vapor
tardy bay
#

I dont know how to code the part where it can not be overdrawn :(

uncut vapor
#

look at toString

#

more specifically, @Override

tardy bay
#

why

uncut vapor
#

thats your goal here

#

when you extend a type, at least for your assignment, you are expected to "override" existing methods/behavior

#

the only time things can be overdrawn is when you try to withdraw, yeah?

uncut vapor
#

so you should override the withdraw method

#

google "java overriding methods"

#

look up some vids maybe, get some exposure to it

tardy bay
tardy bay
uncut vapor
#

yup

tardy bay
#

so does that go where the tostirng is or somewhere else?

uncut vapor
#

you gotta change the method in your subclass, override it

#

change it so it doesnt over withdraw

uncut vapor
tardy bay
uncut vapor
uncut vapor
#

objects, classes, methods

#

so you have to start there

#

because your next assignment wont be any easier

#

so start asking about that stuff

#

once you understand that stuff, the assignment will be a piece of cake

tardy bay
#

i know im def gonna study harder

uncut vapor
#

but youre neglecting it

uncut vapor
tardy bay
#

but i cant tho bc

uncut vapor
#

what is a class?

tardy bay
#

the lab is due at 9 am and i just want to finish it

tardy bay
uncut vapor
#

youll be back with another lab, with the same problem

#

"i put it off last minute, now i just need it done"

#

in 1 hour, your whole view of this can change

#

but you gotta understand the environment. what a class is for, what methods are for, what extending is for

tardy bay
#

i know

uncut vapor
#

so invest the hour in that. put this project aside for an hour, understand "i should have done this before, now i gotta cram it in"

#

then go back in with a better understanding

#

so let me know when you're ready for that

#

cause no one wants to help someone write an essay in a language the person doesnt know

tardy bay
#

i get that

#

i def do not want to be doing this at 3 am tho

uncut vapor
#

rather that than fail?

#

it sucks, but i mean, its whats apparently needed

#

cause the next assignment wont be any easier

#

and we arent gonna do this everytime you have a Java lab

tardy bay
#

well its mostly the profs fault i had to switch schedules due to a stalker and they're not giving me any flexibility for my labs

uncut vapor
#

so invest the hour, itll save you days

uncut vapor
quaint kiln
#

It's better to lose some sleep now, or fail than just have it hand delivered by us.

tardy bay
uncut vapor
#

dont make excuses. in the end, "i dont wanna be up at 3am"

#

you arent giving flexibility

#

you dont even wanna stay up

quaint kiln
#

And in the time this thread's been going on you could easily have looked these matters up.

tardy bay
#

i have looked it up.. i just cant understand it

uncut vapor
#

invest the hour

quaint kiln
#

And even with the stalker, I doubt you got this assignment yesterday.

uncut vapor
#

so let us help, but you gotta start at the basics

quaint kiln
uncut vapor
#

which means putting this project aside for a sec, to practice the basics

quaint kiln
#

Because as it stands it seem like you do not get method overriding (or methods in general?) which needs to be tackled before diving further into inheritance.

uncut vapor
#

thats the goal

#

the solution is right there

quaint kiln
#

That's code, what's unclear about it?

tardy bay
#

what is @override doing

uncut vapor
#

you just dont understand it, because you dont understand methods

uncut vapor
#

what is class doing? what are methods really doing?

#

@Override occurs when you define a subclass (a class which extends another class) & the subclass re-defines a method

#

a method that existed in the class being extended

#

and that probably wont make much sense, because you dont understand methods & classes

tardy bay
#

is this just a print statement?

quaint kiln
#

Why do you think that?

#

What are the parts of that bit of code?

tardy bay
#

the quotations

#

Is this more correct?

quaint kiln
tardy bay
quaint kiln
#

No. You never set your overdraft limit. Your toString's also returning the wrong value.

#

And what if the amount was -20?

tardy bay
quaint kiln
#

And what do you do with that? What's the value? How does it impact my withdrawal limit?

tardy bay
#

would i do something like private int overdraftLimit = 5000;

#

this assingment is difficult bc i dont even know what overdrafting means

quaint kiln
#

That depends on the wording of your assignment.

quaint kiln
#

If you have 200 dollars in your bank account and you try to withdraw 300 dollars.

#

200 > 300.

tardy bay
#

so you cant spend more than you have?

quaint kiln
#

So if your overdraft limit's 0 that would not be possible, if it was 100 or greater it would be.

tardy bay
#

ohhhhhh

#

i get it

tardy bay