Help! Basic java(highschool), need editing

Discussion in 'Software' started by Chuubs, Feb 25, 2013.

  1. Chuubs

    Chuubs Private E-2

    Hi guys!

    I'm in highschool and taking ICS3U1 (Grade 11 Computer Science), I'm extremely new to Java and pretty much only know the basics :confused. So I was wondering if any of you were kind enough to look over my VERY LONG (I apologize in advance) assignment. I'm looking in particular to definitely shorten it, ensure all my declaration of variables are appropriate, and to make it efficient. The program runs, but there are definitely many flaws .. SO IF YOU ARE KIND ENOUGH TO EDIT MY PROGRAM (EVEN A FEW LINES IS FINE!) I WOULD BE EXTREMELY GRATEFUL :D

    CODE:

    import java.util.* ;
    import java.text.* ;

    public class FamilyBudget {

    public static void main (String [] args) {

    double papaSalary, papaIncome, papaTax ;
    double numOfHouse, numOfCar, mamaTax, mamaIncome ;
    double juniorRate, juniorHours, juniorWeek, juniorTax, juniorIncome ;
    double totalIncome, monthlyIncome, totalTax, taxRate, netIncome, avgNet ;

    final double PAPA_TAX = 0.20, MAMA_TAX = 0.15, JUNIOR_TAX = 0.05, MAMA_CAR = 0.098, MAMA_HOUSE = 0.125 ;

    Scanner sc = new Scanner (System.in) ;
    DecimalFormat df = new DecimalFormat ("#.##") ;

    /* Prompting user for information on Papa, Mama and Junior Jackson */
    System.out.print ("Enter Papa Jackson's yearly salary: $ ") ;
    papaSalary = sc.nextDouble () ;
    System.out.print ("Enter the dollar amount of house insurance policies sold by Mama Jackson: $ ");
    numOfHouse = sc.nextDouble () ;
    System.out.print ("Enter the dollar amount of car insurance policies sold by Mama Jackson: $ ");
    numOfCar = sc.nextDouble () ;
    System.out.print ("Enter Junior Jackson's hourly rate: $") ;
    juniorRate = sc.nextDouble () ;
    System.out.print ("Enter the number of hours Junior worked per week: ") ;
    juniorHours = sc.nextDouble () ;
    System.out.print ("Enter the number of weeks Junior worked in 2012: ") ;
    juniorWeek = sc.nextDouble () ;

    /* Calculating incomes and taxes */
    mamaIncome = (numOfHouse * (1 + MAMA_HOUSE) + numOfCar * (1 + MAMA_CAR)) ;
    juniorIncome = (juniorRate * juniorHours) * juniorWeek ;

    papaTax = papaSalary * PAPA_TAX ;
    mamaTax = (numOfHouse * MAMA_HOUSE + numOfCar * MAMA_CAR) * MAMA_TAX ;
    juniorTax = (juniorRate * juniorHours) * juniorWeek * JUNIOR_TAX ;

    totalIncome = papaSalary + (numOfHouse * (1 + MAMA_HOUSE) + numOfCar * (1 + MAMA_CAR)) + juniorIncome ;
    monthlyIncome = totalIncome / 12 ;
    totalTax = papaTax + mamaTax + juniorTax ;
    taxRate = totalTax/totalIncome ;
    netIncome = (papaSalary - papaTax) + (mamaIncome - mamaTax) + (juniorIncome - juniorTax) ;
    avgNet = netIncome / 12 ;

    /* Outputting incomes */
    System.out.println ("\nPapa Jackson's total income for 2012 was $" + df.format (papaSalary)) ;
    System.out.println ("Mama Jackson's total income for 2012 was $" + df.format (mamaIncome)) ;
    System.out.println ("Junior Jackson's total income for 2012 was $" + df.format (juniorIncome)) ;
    System.out.println ("The Jackson family's total income for 2012 was $" + df.format (totalIncome) + " and had an average monthly income of $" + df.format(monthlyIncome)) ;

    /* Outputting taxes */
    System.out.println ("\nIn 2012 Papa Jackson paid $" + df. format (papaTax) + " worth of taxes, while Mama Jackson paid $" + df.format (mamaTax) + " and Junior paid $" + df.format (juniorTax) ) ;
    System.out.println ("Therefore the entire Jackson family paid $" + df.format (totalTax) + " in taxes. ") ;
    System.out.println ("The Jackson's overall tax rate was " + df.format (taxRate) + "%") ;
    System.out.println ("The Jackson's total net income was $" + df. format (netIncome) + " and had an average monthly net income of $" + df. format (avgNet) ) ;

    /* Investment */

    int wholeDollarPortion, numerator, denominator ;
    double amountOfInvestment, numOfShareStock ;

    final double FAMILY_INVEST = 0.08 ;

    /* Prompting user for input */
    System.out.print ("\nEnter the whole dollar portion of your share $") ;
    wholeDollarPortion = sc.nextInt () ;
    System.out.print ("For the price of your stock, enter the numerator of the fraction: ") ;
    numerator = sc.nextInt () ;
    System.out.print ("Now the denominator: ") ;
    denominator = sc.nextInt () ;

    /* Calculating investments */
    amountOfInvestment = netIncome * FAMILY_INVEST ;
    numOfShareStock = amountOfInvestment/((wholeDollarPortion*denominator + numerator)/ denominator) ;

    /* Outputting investment values */
    System.out.println ("\nThe Jackson family invested $" + df.format (amountOfInvestment) + " into their shared shock." );
    System.out.println ("The number of share of stock invested by the Jackson family was $" + df.format (numOfShareStock)) ;

    /* Expenses - Electricity Bill */
    int numOfUnits, numOfMonths ;
    double electricityBillCost, discount ;
    final double UNIT_COST = 0.16, DISCOUNT = 0.90, UTILITY_TAX = 1.02, REBATE = 50 ;

    /* Prompting user for input */
    System.out.print ("\nEnter the number of units of electricity used over the year: ") ;
    numOfUnits = sc.nextInt () ;
    System.out.print ("Enter the number of months the 10% discount is offered: ") ;
    numOfMonths = sc.nextInt () ;

    /* Calculations */
    electricityBillCost = (UNIT_COST * numOfUnits) - 50 ;
    discount = (electricityBillCost / 12) * numOfMonths * DISCOUNT ;
    electricityBillCost = (electricityBillCost - discount) * UTILITY_TAX ;

    /* Outputting values */
    System.out.println ("\nThe Jackson family's electricity bill for 2012 was $" + df.format (electricityBillCost)) ;

    /* Expenses - Phone Bill */

    double numOfMins, numOfBlocks, costOfBlock, phoneCost ;

    /*Prompting user for input */
    System.out.print ("\nEnter the number of phone minutes used in 2012: ") ;
    numOfMins = sc.nextDouble () ;
    System.out.print ("Enter the number of minutes in a block of time: ") ;
    numOfBlocks = sc.nextDouble () ;
    System.out.print ("Enter the amount charged per a block of time: $") ;
    costOfBlock = sc.nextDouble () ;

    /* Calculations */
    phoneCost = (numOfMins/numOfBlocks) * costOfBlock ;

    /* Outputting values */
    System.out.println ("\nThe total cost of the Jackson's phone bill in 2012 was $" + df.format (phoneCost)) ;

    /* Expenses - Gas */
    String brand, type ;
    double distance, km, litre1, dollar, litre2, avgPrice, fuelEfficiency, costOfGas ;

    /* Prompting user for input */
    System.out.print ("\nWhat is the brand of your car? ") ;
    brand = sc.nextLine () ;
    System.out.print ("What is the type of your car? ") ;
    type = sc.nextLine () ;
    System.out.print ("Your car's fuel efficiency is (KM)? ") ;
    km = sc.nextDouble () ;
    System.out.print ("Your car's fuel efficiency is (L)? ") ;
    litre1 = sc.nextDouble () ;
    System.out.print ("The average gas price of 2012 was? $ ") ;
    dollar = sc.nextDouble () ;
    System.out.print ("The average gas price of 2012 in litres was?") ;
    litre2 = sc.nextDouble () ;

    /* Calculations */
    fuelEfficiency = km / litre1 ;
    avgPrice = dollar / litre2 ;
    costOfGas = fuelEfficiency * avgPrice ;

    /* Outputting values */
    System.out.println ("\nThe total cost of gasoline for the Jacksons in 2012 was $" + df.format (costOfGas)) ;


    /* Expenses - Mortgage */
    double mortgagePayment, totalMortgagePay ;

    /* Prompting user for input */
    System.out.print ("\nEnter the amount of each bi-weekly mortgage payment") ;
    mortgagePayment = sc.nextDouble () ;

    /* Calculations */
    totalMortgagePay = mortgagePayment * 26 ;

    /* Outputting values */
    System.out.println ("The total amount of money the Jacksons put towards their mortgage was $" + df.format (totalMortgagePay)) ;

    /* Other Expenses Calculations */
    double groceries, entertainment, cashSaving, moneyRemaining ;

    /* Calculating expenses */
    moneyRemaining = netIncome - amountOfInvestment - electricityBillCost - phoneCost - costOfGas - mortgagePayment ;
    groceries = moneyRemaining * 0.60 ;
    entertainment = moneyRemaining * 0.25 ;
    cashSaving = moneyRemaining * 0.15 ;

    /* Outputting expenses */
    System.out.println ("\nThe Jackson Family after taxes, investments, and expenses spent $" + df.format (groceries) + " on groceries, $" + df.format (entertainment) + " and put $" + cashSaving + " towards their savings.") ;

    double totalExpenses, totalReserve, totalTaxPaid ;

    totalTaxPaid = totalTax + (electricityBillCost * 0.02) ;
    totalExpenses = electricityBillCost + phoneCost + costOfGas + mortgagePayment + groceries + entertainment ;
    totalReserve = cashSaving + amountOfInvestment ;

    }// main method
    }// class FamilyBudget
     
  2. red death68

    red death68 Command Sergeant Major

    I can look at it tommarrow around 8am est and try to clean it up after work. All I require is a detailed explination of what you are trying to accomplish. Also are you trying to give it a gui interface?
     
  3. red death68

    red death68 Command Sergeant Major

    ok I have just started to review your code so far my chief complaint is that everything is inside the main method. I will work on this tomorrow when I have time, however some background on the question or prompt for this program would be nice.
     
  4. red death68

    red death68 Command Sergeant Major

    ok so i finally got around to working on your code I am currently making things into methods and cleaning up your variables I will also add nice comments when I am done. I do have a few questions for you though.

    First is this code editing for personal review and learning or is it being edited for a grade?

    Second do you wish to have an ui for this(will make things nicer but look a bit more advanced in terms of code(will be well commented)

    Third how soon would you like this completed?

    Answers to these three questions would be nice you can also reach me at my instant messenger handles which are shown in my profile just be sure to tell me who you are and why you are messaging me so I don't disregard as spam.
     
  5. red death68

    red death68 Command Sergeant Major

    almost everything is in its own method now. I am working o the variables and cutting back on the amount if at all possible just for neatness will hopefully have this done by monday. Would have been done sooner but I am working on my own program in between this and other aspects of life.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds