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: Exchanging between classes and methods

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Exchanging between classes and methods

    Hey! So, I'm in serious need of help. It should be simple, but I'm over thinking this and need help. It's a program due tonight. I need to create a Dog class that represents a dog. Then, there are 2 different methods that in the end will each represent one dog each. Each printout will show the Name, Breed, Age, and Age in Human Years. Right now I'm having trouble trying to move methods in between each class. Here's the first class.
    import java.util.Scanner;
     
    public class Dog { //no main method
    		static Scanner input = new Scanner(System.in);
     
    	//writeObject method
    	public static void writeObject(String name, String breed, int age) {
    		System.out.println("Enter the dog's name, breed, and age: ");
    		name = input.toString();
    		breed = input.toString();
    		age = input.nextInt();
    	}
    	//getAgeInHumanYears method
    	public static void getAgeInHumanYears(String name, String breed, int age) {
    		System.out.println("Enter the dog's name, breed, and age: ");
    		name = input.toString();
    		breed = input.toString();
    		age = input.nextInt();	
    	}
    }
    And this is the second class code
    public class Dog_Demo {
     
    	public static void main(String[] args) {
    		String name;
    		String breed;
    		int age;
    		int humanAge;
    	}
    	//writeObject method
    	public static String writeObject() {
    		String name, breed;
    		int age;
     
    		System.out.println("Name: ", Dog.writeObject(name, breed, age));
    		return null;
     
    	}
    	//getAgeInHumanYears method
    	public static String getAgeInHumanYears() {
    		System.out.println();
    		return null;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Exchanging between classes and methods

    having trouble trying to move methods in between each class.
    What methods do you want in what class?
    If there are compiler errors, copy and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Exchanging between classes and methods

    The assignment says to have both methods in both classes, but I'm confused on how to assign variables to each of the methods. There's no compiler messages yet because I'm getting stuck on how to compile the code, so it can go through because in Dog.Java, the first code in block #1, it has all the inputs and I want to run those methods in the Dog_Demo.java. I have one problem in this code:
    public static String writeObject() {
    		String name, breed;
    		int age;
     
    		System.out.println("Name: ", Dog.writeObject(name, breed, age));
    		return null;
    	}
    It says: The method println(String) in the type PrintStream is not applicable for the arguments (String, void). So, I change it to:
    System.out.printf("Name: ", Dog.writeObject(name, breed, age));
    		return null;
    But then it says: The method printf(String, Object...) in the type PrintStream is not applicable for the arguments (String, void)
    Change return type of writeObject(...) to Object[]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Exchanging between classes and methods

    If you get error messages you want help with, copy the full text of the error message and paste it here.

    System.out.println("Name: ", Dog.writeObject(name, breed, age));
    The println() method takes ONE String for its argument. The , between the args in the call to the method makes it have two Strings. Replace the , with a + to build a single String to be printed.

    Also to use the writeObject() method as part of the args, the method must return a String, and not be void.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Methods In Different Classes
    By Khadafi in forum Java Theory & Questions
    Replies: 2
    Last Post: January 11th, 2012, 06:38 PM
  3. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  4. classes and methods??
    By paddy1 in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:02 AM
  5. [SOLVED] Help with Classes/ Instance Methods
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 06:33 PM