I am a student who is taking a computer science course in college and have been assigned to write an algorithm for a java program. I have no previous programming experience and I am really behind in this class. It seems fairly simple and anyone on here could probably do it with their eyes closed. I have to write an algorithm dealing with incomes and income taxes so far i have (see below) but do not know where to go from there. As you can see its basic fudamentals of java but i have no clue what i am doing, any help would be greatly appreciated.

Done>false
Read(income)
If(income ==-1)
Done>true
While(!done)
If(income <= 15000)

Dani AI

Generated

A clear, testable plan removes most of the confusion in the thread: read incomes until a sentinel value, validate each input, compute tax with a dedicated routine (so the loop stays simple), print the result and keep optional totals. This expands on 's sketch and on 's point about validating the sentinel (using <= 0 is often safer than a magic -1, unless the assignment requires -1).

Algorithm (pseudocode):

initialize count = 0, totalIncome = 0, totalTax = 0

loop forever:
  prompt and read income
  if income < 0:   // or if income == -1 when professor requires that sentinel
    break
  tax = ComputeTax(income)
  print income and tax (format to 2 decimals)
  count = count + 1
  totalIncome = totalIncome + income
  totalTax = totalTax + tax

if count > 0:
  print totals and averages
else:
  print "no incomes entered"

A compact Java skeleton that follows this structure and uses a bracket table for ComputeTax (swap bracket numbers/rates to match assignment rules):

import java.util.Scanner;

class IncomeTaxCalculator {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    final double SENTINEL = -1.0;   // change to <=0 if preferred
    int count = 0;
    double totalTax = 0.0;

    while (true) {
      System.out.print("Enter income (or -1 to quit): ");
      if (!sc.hasNextDouble()) { sc.next(); continue; }
      double income = sc.nextDouble();
      if (income == SENTINEL) break;
      double tax = computeTax(income);
      System.out.printf("Income: %.2f  Tax: %.2f%n", income, tax);
      count++; totalTax += tax;
    }
    sc.close();
    if (count > 0) System.out.printf("Average tax: %.2f%n", totalTax / count);
  }

  private static double computeTax(double income) {
    double[] limits = {10000, 30000, 60000};         // example cutoffs
    double[] rates  = {0.00, 0.10, 0.20, 0.30};     // length = limits.length + 1
    double tax = 0.0;
    double lower = 0.0;
    for (int i = 0; i < limits.length; i++) {
      double taxable = Math.min(income, limits[i]) - lower;
      if (taxable > 0) tax += taxable * rates[i];
      lower = limits[i];
    }
    if (income > lower) tax += (income - lower) * rates[rates.length - 1];
    return tax;
  }
}

Notes: validate non-numeric input with hasNextDouble(), format output to two decimals, and use BigDecimal only if exact currency math is required. Test with small values (below first bracket), mid-range, and large incomes.

Recommended Answers

All 3 Replies

This is not java. And during your class, have you learned to write classes with main method and run the "Hello World" program?

class Hello {
 public static void main(String [] args) {
  system.out.println(Hello World);
 }
}

I know this is not java but my professor wants a simple algorithm written for our program before we actually start writing code for it. no ive never been taught the hello world program

Done>false
Read(income)
If(income ==-1)
Done>true
While(!done)
If(income <= 15000)

Done>false
While(!done)
Read(income)
If(income ==-1)
Done>true
Else
//calculate the income tax
EndIf
EndWhile


First you go inside the while loop:
Done>false
While(!done)

EndWhile


Then you read the income. If it is equals to -1 then done is set to true so you will leave the loop. Else do the calculations and repeat. But I would suggest to change the: '==-1' to '<=0'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.