Results 1 to 4 of 4
  1. #1
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Help calling a method please

    Ok so I have done enough research to try to figure this out myself before I posted here, but I feel I am just missing something simple.
    I am trying to call a method. My main app is:
    InterestCalculatorApp.java and I am trying to call MarcoHeading.java
    (which is used to display my name, current date, and assignment.)
    Here is the method I am trying to call:
    method code:
    public class MarcoHeading {
    private static String firstName = "Charles";
    private static String lastName = "Marco";

    // Add null constructor
    public MarcoHeading() {}

    public static void main(String [] args) {
    getHeading("Testing the personal heading class at " + getTime());
    } // end main

    public static void getHeading(String x) {
    String fromAssignment = x;
    System.out.println("\n\n " + getName());
    System.out.println(" " + fromAssignment);
    System.out.println(" " + getDate());
    System.out.println();
    } // end getHeading()

    public static String getName() {
    return getFirstName() + " " + getLastName();
    }

    public static String getFirstName() {
    return firstName;
    } // end getFirstName()

    public static String getLastName() {
    return lastName;
    } // end getLastName()

    public static String getDate() {
    String date;
    Date now = new Date();
    DateFormat longDate = DateFormat.getDateInstance(DateFormat.LONG);
    return date = longDate.format(now);
    } // end getDate()

    public static String getTime() {
    String time;
    Date nowTime = new Date();
    DateFormat longTime = DateFormat.getTimeInstance(DateFormat.SHORT);
    return time = longTime.format(nowTime);
    } // end getTime()

    } // end MarcoHeading
    *******************************************
    And here is what I am trying to call from: (Any tips advice is greatly appreciated)
    *******************************************
    import java.util.Scanner;
    import java.text.NumberFormat;

    public class InterestCalculatorApp {
    MarcoHeading getHeading = new MarcoHeading();
    Sring.getHeading("Assignment2");
    public static void main(String[] args) {
    // display a welcome message
    System.out.println("Welcome to the Interest Calculator\n");

    // create the Scanner object
    Scanner sc = new Scanner(System.in);

    // perform conversions until choice isn't equal to "y" or "Y"
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

    // get the loan information from the user
    System.out.print("Enter loan amount: ");
    double loanAmount = sc.nextDouble();
    System.out.print("Enter interest rate: ");
    double interestRate = sc.nextDouble();
    System.out.println();

    // calculate the interest amount
    double interest = loanAmount * interestRate;

    // format setups
    NumberFormat percent = NumberFormat.getPercentInstance();
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    // display the results
    System.out.println("Loan amount: " + currency.format(loanAmount));
    System.out.println("Interest rate: " + percent.format(interestRate));
    System.out.println("Interest: " + currency.format(interest));
    System.out.println();

    // see if the user wants to continue
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();
    }
    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    since all your methods inside the class MarcoHeading are static you don't need an instance to call its methods. anyway, running the code i get this output (with the locale set to german)

    Charles Marco
    Testing the personal heading class at 17:01
    18. März 2011

  3. #3
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok, so what is the best way to call the method MarcoHeading.
    I have tried several different options and cant get it to compile or run properly
    I tried the below and several others:
    code:
    ********
    import java.util.Scanner;
    import java.text.NumberFormat;

    public class InterestCalculatorApp {
    public static void main(String[] args) {
    MarcoHeading test1 = new MarcoHeading();
    test1.getHeading();
    *******

    Any ideas?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,068
    Blog Entries
    3
    Rep Power
    14

    Default

    Java Code:
    public static void getHeading(String x)
    This is your getHeading method, there are two things you need to notice.
    1. it's static, see what j2me64 posted.
    2. It takes a String argument.

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •