Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:48:01+00:00 2026-05-27T03:48:01+00:00

I am very new to java and I am having some issues with getting

  • 0

I am very new to java and I am having some issues with getting my code to catch and display an exception has been caught. The idea of the code is to throw an exception if any of the values of Loan are zero or less. Below is my code, which does not have any output for an exception, despite the fact that loan1 and loan3 have nothing but zeros. Any help would be appreciated by a six week newbie. Thanks!

package loan;

public class Loan {

    public static void main(String[] args) {
        try {
        Loan loan1 = new Loan(0, 0, 00.00);
        Loan loan2 = new Loan(5, 10, 500.00);
        Loan loan3 = new Loan(0, 0, 0.00);

        }
        catch (IllegalArgumentException ex) {
                System.out.println(ex);
        }
    }


    // declare variables for Loan class
    double annualInterestRate;
    int numberOfYears;
    double loanAmount;
    private java.util.Date loanDate;

    // Loan class
    public Loan() {
    }
    public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }


    public double getAnnualInterestRate() throws IllegalArgumentException {              //exception added if value is zero or less, added on each variable
        if (annualInterestRate >= 0)
        throw new IllegalArgumentException("Interest rate cannot be zero or          negative.");
        else
            return annualInterestRate;

    }

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

    public int getNumberOfYears() throws IllegalArgumentException {
        if (numberOfYears >= 0)
            return numberOfYears;
        else
            throw new IllegalArgumentException("Number of years cannot be zero");
        }

    public void setNumberOfYears(int numberOfYears) {
        this.numberOfYears = numberOfYears;
    }

    public double getLoanAmount() throws IllegalArgumentException {
        if (loanAmount >= 0)
            return loanAmount;
        else
            throw new IllegalArgumentException("Loan amount cannot be zero");
    }

    public void setLoanAmount(double loanAmount) {
        this.loanAmount = loanAmount;
    }

    public double getMonthlyPayment() {
        double monthlyInterestRate = annualInterestRate/1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1/(1 + monthlyInterestRate), numberOfYears *12)));
        return monthlyPayment;
        }
    public double getTotalPayment() {
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
    }

    public java.util.Date getLoanDate() {
        return loanDate;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:48:01+00:00Added an answer on May 27, 2026 at 3:48 am

    You only throw an exception in the “getter” methods, like getAnnualInterestRate, etc. But you never call them. Either call some of your getters, or (better) throw an exception from the constructor and the setter methods (because this is where you’re setting the values).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Java and am having some issues looping through JCheckBoxes on
I've been trying to write some very fast Java code that has to do
I'm new to java and having some problems. The main idea is to connect
I'm fairly new to Java and I've been having some difficulties with Swing. I'm
I'm very new to Java but I've been developing a habit to use final
I'm relatively new to java (specifically swing) and have recently been making some fairly
I am very new to Java, I want to add a PNG image to
I am very new to processing.org and Java. I am trying to store objects
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm soon going to check in the very first commit of a new Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.