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 4 of 4

Thread: Array Code Help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array Code Help

    public class Kitty {
     
        private String name;
        private int age;
     
        public Kitty(){
        }
     
        public void setName(String n){
            name = n;
        }
     
        public String getName(){
            return name;
        }
     
        public void setAge(int a){
            age = a;
        }
     
        public int getAge(){
            return age;
        }
    }

    The array myCats is an 8 element array of type Kitty that has already been declared and initialized. Write the expression(s) in the box below that prints to the console the name of the fourth Kitty if its age is strictly greater than the age of the fifth Kitty, and prints the name of the fifth Kitty otherwise:

    (Remember: the first element of an array is at index position 0).

    My code is
    int age4=myCats[3].getAge();
    int age5=myCats[4].getAge();
    if(age4>age5)
    {
    System.out.print(myCats[3].getName());


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Array Code Help

    And your question is?
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array Code Help

    why isnt it working? this is the feedback i get
    Your code did not print the correct name given this input array:
    Name: Sylvester
    Age: 0
    Name: Sasha
    Age: 8
    Name: Toby
    Age: 2
    Name: Felix
    Age: 3
    Name: Toby
    Age: 7
    Name: Buddy
    Age: 10
    Name: Fuzzball
    Age: 13
    Name: Jasmine
    Age: 7
    Your code printed this name: ,
    the correct name is Toby.
    Remember, print the name of the 4th Kitty if it is strictly greater then the 5th Kit

  4. #4
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Array Code Help

    Make sure you implement an "else" statement
    Last edited by vanDarg; March 21st, 2011 at 04:46 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. Duplicating array in C code
    By FearTheCron in forum Java Native Interface
    Replies: 1
    Last Post: April 6th, 2013, 09:33 PM
  3. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 11:54 AM
  4. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM
  5. Need help with array code
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 10:54 PM