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

Thread: Help Calling Method From Another Class

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help Calling Method From Another Class

    Hello again!
    Can anybody help me figure out how to call my getActivity method from my student class in my app class? I'm trying display how much a student does a certain activity, but my loop goes on forever and doesn't stop at 20.

    Student class
    public class student {
    	String firstName;
    	String lastName;
    	int age;
    	student(String informedFirst, String informedLast, int informedAge)
    	{
    		firstName=informedFirst;
    		lastName=informedLast;
    		age=informedAge;
    	}
    	String getName()
    	{
    	return firstName + " " +lastName;
    	}
    	String getInfo()
    	{
    		return firstName + " " +lastName+" "+age;
    	}
    	String getActivity()
    	{
    		int myNumber = (int) (Math.random() * 3);
    		String result = " ";
    		if(myNumber == 0)
    		result = "Reading";
    		if(myNumber == 1)
    		result = "Programming";
    		if(myNumber == 2)
    			result = "Surfing";
    		return result;
    	}
    }

    App class
    public class App {
    	public static void main(String args[])
    	{
    		student st1=new student("Zack","Mills",20);
     
    		int countReading=0;
    		int countProgramming=0;
    		int countSurfing=0;
    		for(int i=0;i<20;i++)
    		{
    			double outcome =Math.random()*3;
    			if(outcome == 0)
    				countReading++;
    			else
    				if (outcome ==1)
    					countProgramming++;
    				else
    					if(outcome ==2)
    						countSurfing++;
    			String action=st1.getActivity();
    		}
    		System.out.println("Zack is"+st1.getActivity()+ countReading*100/20+"%");
    	}
    }


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Help Calling Method From Another Class

    Your program is correct. Its running and stopping after 20 times. The output is always 0 as you are matching a double value with integer value which will never be true.

    So you change your code as follows:
    if((int)outcome == 0)
    countReading++;
    else
    if ((int)outcome ==1)
    countProgramming++;
    else
    if((int)outcome ==2)
    countSurfing++;


    I am here typecasting the outcome from double to integer. Click on thanks link

  3. The Following User Says Thank You to jassi For This Useful Post:

    CheekySpoon (February 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help Calling Method From Another Class

    Thanks a bunch. I had not realized I was missing the int. It works perfectly now!

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  3. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  4. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM