• Log in or sign up for an account here: https://www.sythe.org/login

Reported Post by Trent!

Status
Not open for further replies.

Trent!

Guru

$5 USD Donor New
Joined
Aug 21, 2010
Sythe Gold
7
Messages
1,845
Two Factor Authentication User
Trent! has reported a post.

Reason:
Can this please be deleted.
Post: Basic Java Help Needed!
Forum: Programming General
Assigned Moderators: yaroow, Shoop, malakadang

Posted by: Trent!
Original Content:
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:
4RSPw.png


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!!!
 
St. Patrick's Day 2013
Two Factor Authentication User
Easter 2013
Heidy
Homosex
Pizza Muncher
I've closed it.
 
Status
Not open for further replies.
Runescape Gamblingrunescape gambling
Back
Top