Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot get my head around this :S

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot get my head around this :S

    For my college summer task I have been given this as question two, I simply cannot get my head around it!


    Create a class Exercise2 with the code below:

    import java.util.Scanner;  //package needed to read from keyboard
    public class Exercise2
    {
       public static void main (String[] args)
       {
           //declare Scanner and integer variables
           Scanner sc = new Scanner(System.in);
           int allMonths, years, partMonths;
     
           //ask for age in months
           System.out.print("Age in months: ");
     
           //set allMonths to value typed in
           //code here
     
           //calculate years from allMonths
           //code here
           //calculate partMonths from allMonths
           //code here
     
           //display message about calculated years and months
           //code here
       }//end main
    }//end class


    Currently the program prompts the user for a number of months but does not yet calculate and output how many years and months this is equivalent to. For example when the user types an input of 40 the program output should be:
    Age in months: 40
    40 months = 3 years and 4 months

    What you need to do
    Modify the program so that for an input number of months the whole years and months are calculated and displayed

    Test your program with age in months values as follows.
    Test 1: 11
    Test 2: 24
    Test 3: 30

    ---------------------------------------------------------------
    I have tried and got this so far,
    ---------------------------------------------------------------


    import java.util.Scanner;  //package needed to read from keyboard
    public class Exercise2
    {
       public static void main (String[] args)
       {
           //declare Scanner and integer variables
           Scanner scan = new Scanner (System.in);
           int allMonths, years, partMonths;
     
           //ask for age in months
           String inData;
     
           System.out.println("Age in months: ");
           inData = scan.nextLine();
     
           //set allMonths to value typed in
     
     
           //calculate years from allMonths
           //code here
           //calculate partMonths from allMonths
           //code here
     
           //display message about calculated years and months
           //code here
       }//end main
    }//end class
    Not much I know, but atleast i'm trying! Some help would be greatly appreciated!
    Last edited by hillzyy; July 17th, 2012 at 01:14 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot get my head around this :S

    Duplicate of http://www.javaprogrammingforums.com...ect-stuck.html Please read the forum rules. I am locking this thread.

Similar Threads

  1. Head First Servlets and JSP 2e Opinions Please
    By djl1990 in forum Java Servlet
    Replies: 0
    Last Post: May 7th, 2012, 06:01 PM
  2. Easy but over my head
    By Dejay79 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2012, 10:54 AM
  3. Record audio from mic and head phone
    By yoru214 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 17th, 2011, 07:38 AM
  4. [SOLVED] My head Hurts... such a simple problem but I'm stuck!
    By Kassino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2010, 08:38 AM
  5. Simple Game In Java (Head and Tails).
    By drkossa in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2009, 11:40 AM