Basic Java Help Needed!

Discussion in 'Programming General' started by Trent!, Oct 16, 2013.

Thread Status:
Not open for further replies.
Basic Java Help Needed!
  1. Unread #1 - Oct 16, 2013 at 7:57 PM
  2. Trent!
    Joined:
    Aug 21, 2010
    Posts:
    1,845
    Referrals:
    2
    Sythe Gold:
    7
    Two Factor Authentication User

    Trent! Guru
    $5 USD Donor New

    Basic Java Help Needed!

    Howdy, so basically I've been given this assignment in my Java class to construct a script to convert the years of humans and 4 other animals into the context of the other animals (Ex, 1 human year = 2.16 Hippo years). I have the entire program written properly, the only problem I'm having is that one of the requirements is to create at least one other method to reduce the amount of repetitive code. I've struggled with doing this bit since the class started, and all of the example we've worked on only handle one parameter.

    Anyway, my goal is to turn the chunk of code that handles the calculations into a method of its own, that will work with the rest of the code.

    Code:
    // AgeInHippoYears.java
    import java.util.Scanner;
    
    public class AgeInHippoYears
    {
    // Take input from the user to calculate the age equivalent
    // of various animals in comparison to other animals.
    
    public static void main(String[] args)
    { 
    final float HUMAN_C = 100000;
    final float HIPPO_C = 46154;
    final float BEAR_C = 35385;
    final float HAMSTER_C = 3077;
    final float CAT_C = 16923;
    float targetAge;
    
    Scanner scan = new Scanner(System.in);
    
    System.out.print("Enter the target age (integers only!): ");
    targetAge = scan.nextInt();
    
    float humanHuman = targetAge * (HUMAN_C / HUMAN_C);
    float humanBear = targetAge * (HUMAN_C / BEAR_C);
    float humanHamster = targetAge * (HUMAN_C / HAMSTER_C);
    float humanCat = targetAge * (HUMAN_C / CAT_C);
    float humanHippo = targetAge * (HUMAN_C / HIPPO_C);
    
    float bearHuman = targetAge * (BEAR_C / HUMAN_C);
    float bearBear = targetAge * (BEAR_C /BEAR_C );
    float bearHamster = targetAge * (BEAR_C /HAMSTER_C );
    float bearCat = targetAge * (BEAR_C /CAT_C );
    float bearHippo = targetAge * (BEAR_C / HIPPO_C);
    
    float hamsterHuman = targetAge * (HAMSTER_C / HUMAN_C);
    float hamsterBear = targetAge * (HAMSTER_C / BEAR_C);
    float hamsterHamster = targetAge * (HAMSTER_C / HAMSTER_C);
    float hamsterCat = targetAge * (HAMSTER_C / CAT_C);
    float hamsterHippo = targetAge * (HAMSTER_C / HIPPO_C);
    
    float catHuman = targetAge * (CAT_C / HUMAN_C);
    float catBear = targetAge * (CAT_C / BEAR_C);
    float catHamster = targetAge * (CAT_C / HAMSTER_C);
    float catCat = targetAge * (CAT_C / CAT_C);
    float catHippo = targetAge * (CAT_C / HIPPO_C);
    
    float hippoHuman = targetAge * (HIPPO_C / HUMAN_C);
    float hippoBear = targetAge * (HIPPO_C / BEAR_C);
    float hippoHamster = targetAge * (HIPPO_C / HAMSTER_C);
    float hippoCat = targetAge * (HIPPO_C / CAT_C);
    float hippoHippo = targetAge * (HIPPO_C / HIPPO_C);
    
    String table = "\t║ %9.2f ║ %9.2f ║ %9.2f ║ %9.2f ║ %9.2f ║\n";
    String sTable = "\t\t\t\t║ %7s ║ %7s ║ %7s ║%7s ║ %7s ║\n";
    
    System.out.println("\t\t\t\t\tA(n) " + targetAge + " year old...");
    System.out.println("\t\t\t\t╔═══════════╦═══════════╦═══════════╦═══════════╦═══════════╗");
    System.out.printf(sTable, "Human", "Bear", "Hamster", "Cat", "Hippo");
    System.out.println(" is like a __ year old...\t╠═══════════╬═══════════╬═══════════╬═══════════╬═══════════╣");
    System.out.printf("\t\t\tHuman" + table, humanHuman, humanBear, humanHamster, humanCat, humanHippo);
    System.out.printf("\t\t\tBear" + table, bearHuman, bearBear, bearHamster, bearCat, bearHippo); 
    System.out.printf("\t\t\tHamster" + table, hamsterHuman, hamsterBear, hamsterHamster, hamsterCat, hamsterHippo);
    System.out.printf("\t\t\tCat" + table, catHuman, catBear, catHamster, catCat, catHippo);
    System.out.printf("\t\t\tHippo" + table, hippoHuman, hippoBear, hippoHamster, hippoCat, hippoHippo);
    System.out.println("\t\t\t\t╚═══════════╩═══════════╩═══════════╩═══════════╩═══════════╝");
    }
    
    
    }
    
    This is the printout:
    [​IMG]

    Basically I want to make a method of its own for the calculations that will work properly with the printout. Even just one example that will show me how I would do this for the rest of it would make me happy. Any help is greatly appreciated!!!
     
  3. Unread #2 - Oct 16, 2013 at 8:37 PM
  4. bloodymager
    Joined:
    Mar 28, 2009
    Posts:
    1,296
    Referrals:
    1
    Sythe Gold:
    0

    bloodymager Guru
    Banned

    Basic Java Help Needed!

    // AgeInHippoYears.java
    import java.util.Scanner;

    public class AgeInHippoYears
    {
    // Take input from the user to calculate the age equivalent
    // of various animals in comparison to other animals.

    public static void main(String[] args)
    {
    final float HUMAN_C = 100000;
    final float HIPPO_C = 46154;
    final float BEAR_C = 35385;
    final float HAMSTER_C = 3077;
    final float CAT_C = 16923;
    float targetAge;

    Scanner scan = new Scanner(System.in);

    System.out.print("Enter the target age (integers only!): ");
    targetAge = scan.nextInt();
    printYears(targetAge);
    }
    public void printYears(int targetAge){
    float humanHuman = targetAge * (HUMAN_C / HUMAN_C);
    float humanBear = targetAge * (HUMAN_C / BEAR_C);
    float humanHamster = targetAge * (HUMAN_C / HAMSTER_C);
    float humanCat = targetAge * (HUMAN_C / CAT_C);
    float humanHippo = targetAge * (HUMAN_C / HIPPO_C);

    float bearHuman = targetAge * (BEAR_C / HUMAN_C);
    float bearBear = targetAge * (BEAR_C /BEAR_C );
    float bearHamster = targetAge * (BEAR_C /HAMSTER_C );
    float bearCat = targetAge * (BEAR_C /CAT_C );
    float bearHippo = targetAge * (BEAR_C / HIPPO_C);

    float hamsterHuman = targetAge * (HAMSTER_C / HUMAN_C);
    float hamsterBear = targetAge * (HAMSTER_C / BEAR_C);
    float hamsterHamster = targetAge * (HAMSTER_C / HAMSTER_C);
    float hamsterCat = targetAge * (HAMSTER_C / CAT_C);
    float hamsterHippo = targetAge * (HAMSTER_C / HIPPO_C);

    float catHuman = targetAge * (CAT_C / HUMAN_C);
    float catBear = targetAge * (CAT_C / BEAR_C);
    float catHamster = targetAge * (CAT_C / HAMSTER_C);
    float catCat = targetAge * (CAT_C / CAT_C);
    float catHippo = targetAge * (CAT_C / HIPPO_C);

    float hippoHuman = targetAge * (HIPPO_C / HUMAN_C);
    float hippoBear = targetAge * (HIPPO_C / BEAR_C);
    float hippoHamster = targetAge * (HIPPO_C / HAMSTER_C);
    float hippoCat = targetAge * (HIPPO_C / CAT_C);
    float hippoHippo = targetAge * (HIPPO_C / HIPPO_C);

    String table = "\t║ %9.2f ║ %9.2f ║ %9.2f ║ %9.2f ║ %9.2f ║\n";
    String sTable = "\t\t\t\t║ %7s ║ %7s ║ %7s ║%7s ║ %7s ║\n";

    System.out.println("\t\t\t\t\tA(n) " + targetAge + " year old...");
    System.out.println("\t\t\t\t╔═══════════╦═══════════╦═══════════╦═══════════╦═══════════╗");
    System.out.printf(sTable, "Human", "Bear", "Hamster", "Cat", "Hippo");
    System.out.println(" is like a __ year old...\t╠═══════════╬═══════════╬═══════════╬═══════════╬═══════════╣");
    System.out.printf("\t\t\tHuman" + table, humanHuman, humanBear, humanHamster, humanCat, humanHippo);
    System.out.printf("\t\t\tBear" + table, bearHuman, bearBear, bearHamster, bearCat, bearHippo);
    System.out.printf("\t\t\tHamster" + table, hamsterHuman, hamsterBear, hamsterHamster, hamsterCat, hamsterHippo);
    System.out.printf("\t\t\tCat" + table, catHuman, catBear, catHamster, catCat, catHippo);
    System.out.printf("\t\t\tHippo" + table, hippoHuman, hippoBear, hippoHamster, hippoCat, hippoHippo);
    System.out.println("\t\t\t\t╚═══════════╩═══════════╩═══════════╩═══════════╩═══════════╝");

    }


    }
     
  5. Unread #3 - Oct 16, 2013 at 9:23 PM
  6. Trent!
    Joined:
    Aug 21, 2010
    Posts:
    1,845
    Referrals:
    2
    Sythe Gold:
    7
    Two Factor Authentication User

    Trent! Guru
    $5 USD Donor New

    Basic Java Help Needed!


    I appreciate the effort, however that will not compile. The error was related to the printYears(targetAge); being non-static within a static method.
     
  7. Unread #4 - Oct 16, 2013 at 11:48 PM
  8. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Basic Java Help Needed!

    Tell me what exactly you want the method to do and I can help you make the method. If they are all unique calculations I am not sure how creating a method to do the calculations would be any less redundant.
     
  9. Unread #5 - Oct 19, 2013 at 11:26 AM
  10. Shoop
    Joined:
    Feb 14, 2010
    Posts:
    4,418
    Referrals:
    0
    Sythe Gold:
    2
    Vouch Thread:
    Click Here
    Discord Unique ID:
    625378835759628290
    Two Factor Authentication User St. Patrick's Day 2013 Pizza Muncher Easter 2013 Homosex Heidy

    Shoop Legend
    $100 USD Donor New Angelic Retired Sectional Moderator

    Basic Java Help Needed!

    Closed.
     
< Entering The Game Industry | Need help writing a script [Python] >

Users viewing this thread
1 guest
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site