#OOP

1 messages · Page 1 of 1 (latest)

winged shell
#

Is this how to initialize variables and do a throw catch statement for if the interest rate is negative?

rough garnetBOT
#

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

rough garnetBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Yes, you can initialize variables and use a try-catch statement to handle exceptions when the interest rate is negative in Java. Here's an example:

public class BankAccount {
    private double balance;
    private double interestRate;

    public BankAccount(double balance, double interestRate) throws IllegalArgumentException {
        if (interestRate < 0) {
            throw new IllegalArgumentException("Interest rate cannot be negative");
        }
        this.balance = balance;
        this.interestRate = interestRate;
    }

    // Other methods and code for the BankAccount class...
}

In the above example, we have a BankAccount class with two instance variables: balance and interestRate. The constructor of the class takes these two parameters and throws an IllegalArgumentException if the interest rate is negative.

You can then create an instance of the BankAccount class as follows:

try {
    BankAccount account = new BankAccount(1000.0, -0.5);
} catch (IllegalArgumentException e) {
    System.out.println(e.getMessage());
}

In this code snippet, we create a new BankAccount object with a balance of 1000.0 and an interest rate of -0.5. Since the interest rate is negative, it will throw an exception which we catch using a try-catch block. The error message will be printed to the console.

Note that it's generally recommended to use meaningful exception types instead of generic ones like IllegalArgumentException. You can create your own custom exception classes to provide more specific information about the error.

lofty jetty
#

no

#

you're assigning the "annualInterestRate" parameter's value to the annualInterestRate field

#

and the try catch block just says "if anything in this try {} block throws an IllegalArgumentException, save it in Negative and run the catch {} block"

#

to throw an exception, use throw new ExceptionName("exception details, optional");

lofty jetty
#

in this case, you probably want to do something like this: ```java
public CreditAccount(..., double annualInterestRate) {
if (annualInterestRate < 0) throw new IllegalArgumentException("Interest rate cannot be negative");
// ...
}

rough garnetBOT
lofty jetty
#

variables defined on a class, to be more specific

#

variables defined in a method are called "local" variables, and variables on classes are called fields

winged shell
#

isn't assigning parameter values to the method's variables setting

#

i'm not sure how i should initialize

lofty jetty
#

initializing fields isn't necessary, fields always have a default value

#

you're assigning values in this case

#

with final fields, this is a bit different, you need to initialize those since they can only be initialized once

winged shell
#

oh

#

but the assignment told me to initialize

#

wait wait wait

lofty jetty
#

terminology varies, especially around programming tutorials

#

you're familiar with initializing, so they called it that

winged shell
#

okay

#

so what uh

#

what is this asking for again?

#

like what is the method for

lofty jetty
#

it's asking you to throw an IllegalArgumentException if annualInterestRate is negative

#

you want to use the throw keyword

winged shell
#

okay got it but

#

what about that first bit

lofty jetty
#

terminology you know about

#

you're familiar with those words and know what they mean, more or less

#

a more common description for doing what it's asking you to do would be "assign values to the fields, initializing the object"

winged shell
#

there would have to be a new creditaccount object, then though, right?

lofty jetty
#

correct

#

the constructor is responsible for initializing a new CreditAccount

#

each time you call that constructor, java makes a new instance of CreditAccount, and calls the constructor to set it up

winged shell
#

OHHHHHH

lofty jetty
#

the constructor can, if you're playing fair, only run once and always results in a new object

#

new CreditAccount("a name", 10_000.0, 10) would call that constructor you're currently working on and use it to set up a new CreditAccount. I could then use the "return" value of that new expression to save the object your constructor "made"

winged shell
#

when you set parameters that are inputted from an outside source into new objects what do you do

#

because i have a junit tester that makes the object and the parameters i think