call a method from another to a class

This is a discussion on call a method from another to a class within the Tech Board forums, part of the Community Boards category; hey peeps, i have a really big probelemo...i'm in grade 11 and writing i.t p1 prac on monday and still ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    call a method from another to a class

    hey peeps, i have a really big probelemo...i'm in grade 11 and writing i.t p1 prac on monday and still dunno how to call a method from another to a class.....im trying to create a simple program........
    Code:
     
    public class Display
     {
      void display(String heading)
       {
    	 System.out.println("My name is bob.");
       }
     }

    then call it from this class
    Code:
     
    public class CallDisplay
     {
      public static void main(String[]args)
       {
    	 Display print = new Display();
    	 print.display();
    	 System.out.print(print);
    	}
     }
    please help.....eish im stressing lol

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    hey peeps, i have a really big probelemo...i'm in grade 11 and writing i.t p1 prac on monday and still dunno how to call a method from another to a class.....im trying to create a simple program........
    [Code]
    public class Display
    {
    void display(String heading)
    {
    System.out.println("My name is bob.");
    }
    }
    [\code]
    then call it from this class
    Code:
     
    public class CallDisplay
     {
      public static void main(String[]args)
       {
    	 Display print = new Display();
    	 print.display();
    	 System.out.print(print);
    	}
     }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by vipur View Post
    hey peeps, i have a really big probelemo...i'm in grade 11 and writing i.t p1 prac on monday and still dunno how to call a method from another to a class.....im trying to create a simple program........
    Vipur, two things you have to know to work forums like these:

    1) Start your *own* thread, for your problem. With very few exceptions, you'll be way better off.

    2) Get into the *right* forum.

    This is the C forum, not the C++ forum, and C doesn't have classes at all.

    Get over to our C++ forum.

    Eman:
    ====


    For school assignments, I prefer Borland Turbo C/C++, myself. That's what I used to work on your program, btw.

    I believe if you add a few things to the program I pared down for you, you'll have better luck than trying to use the bloated program, as the "backbone", and just trim it down and make it work right.

    I killed it (your old program), at least once, trying to get it whittled down in size.

    If you have any further questions, post up your current version so I'll know where you're at.
    Last edited by Adak; 11-14-2009 at 03:47 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And *AGAIN*, you're in the WRONG FORUM!

    And don't hijack other posters threads - start your own, and do it in the right forum (C++).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Adak
    And don't hijack other posters threads - start your own, and do it in the right forum (C++).
    Agreed, but this looks more like Java than C, C++ or C#.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    Vipur, two things you have to know to work forums like these:

    1) Start your *own* thread, for your problem. With very few exceptions, you'll be way better off.

    2) Get into the *right* forum.

    This is the C forum, not the C++ forum, and C doesn't have classes at all.

    Get over to our C++ forum.

    Eman:
    ====


    For school assignments, I prefer Borland Turbo C/C++, myself. That's what I used to work on your program, btw.

    I believe if you add a few things to the program I pared down for you, you'll have better luck than trying to use the bloated program, as the "backbone", and just trim it down and make it work right.

    I killed it (your old program), at least once, trying to get it whittled down in size.

    If you have any further questions, post up your current version so I'll know where you're at.
    Finally found the thread..How did u kill my program? lol
    I have an idea..what if i use cases for num1 like
    case 1:
    for()
    case2:
    for()
    and generate different questions..but the cases would have different operators.like case1, plus, case 2, minus or times and so on..
    at least that way..the code would be way shorter than before..what dou think?

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Adak View Post
    Vipur, two things you have to know to work forums like these:

    1) Start your *own* thread, for your problem. With very few exceptions, you'll be way better off.

    2) Get into the *right* forum.

    This is the C forum, not the C++ forum, and C doesn't have classes at all.

    Get over to our C++ forum.

    Eman:
    ====


    For school assignments, I prefer Borland Turbo C/C++, myself. That's what I used to work on your program, btw.

    I believe if you add a few things to the program I pared down for you, you'll have better luck than trying to use the bloated program, as the "backbone", and just trim it down and make it work right.

    I killed it (your old program), at least once, trying to get it whittled down in size.

    If you have any further questions, post up your current version so I'll know where you're at.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    /*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
    main() 
    {
        int i ;
        int one=2, two=3, three=4, four=5 ;
        int answer1 = 2 ;
        int answer2 = 9 ;
        int answer3 = 24;
        int answer4 = 100;
        char yesno ;
        int num1 = 0;
        int option = 0 ;
        do
        {
                  
                                printf("         \n   ") ;
                                printf("      ((----))\n") ;
                                printf("      <--<\\\\_//>->\n") ;
                                printf("           | |   \n") ;
                                printf("          (===)\n") ;
                                printf("\n") ;
                                printf("    QUIZZITCH  CUP\n") ;//This is just some drawing of the prize cup that the winner would have
        
                printf("\n") ;
            
        
            printf("1. Enter the number of questions to be asked for this round of the quiz\n") ; 
            printf("2. Start quiz\n") ;
            printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
            printf("4. Exit Program\n") ;
            scanf("%d", &option) ;//waits for an input for 1, 2, 3 or 4
          
            while(option != 1&&option!=4&&option!=3) //if option is not =1 it willforce user to enter1. but if user chooses 3..it would go to the other while loop//this enables it to display the results of option3 if variable num1 isn't empty. if it is empty and user chose 3..then menu would be redisplayed
            {
                printf("You must choose 1 before you can proceed to quiz\n") ;
                scanf("%d", &option) ;
                
                if(option==4)
                {
                    return 0 ;
                    break ;
                }
                  
                i++ ;
    
            }    
           
            switch(option) //the switch option allows me to choose number of questions i want .
            {
                case 1:
                {
                    
                    printf("1. One question\n") ;
              
                    printf("2. Two questions\n") ; 
                
                    printf("3. Three questions\n") ;
               
                    printf("4. Four questions\n") ; 
                
                    scanf("%d", &num1) ; //this scanf to read in the option for the number questions to be answered
                    while(num1 != 1 && num1 != 2&& num1 != 3 && num1 != 4)
                    {
                        printf("You must choose from 1-4 before you can proceed to quiz\n") ;
                        scanf("%d", &num1) ;
                        i++ ;
                    }
                    clrscr() ;
                }
                
                
                case 2: 
                {
                    
                    switch(num1)//start of option 2 using num1 as a variable of numbers of questions to be asked 
                    {
                        case 1:
                        {
                            printf("What is 1+1: *\n") ;
                            scanf("%d", &answer1) ;//the answer is stored in a variable one, two, three, four, and matched with answer1-5. if it matches..then result is display
                           
                            break;
                        }//end of case 1 of question 1 
                        
                        case 2:
                        {   
                            for(i=0; i<num1; one++, i++)
                            {
                                printf("what is %d * %d\n", one, (i+1*3)) ;
                                scanf("%d", &answer2) ;
                            }
                            break ;
                        }//end of question 2
                        //case 3
                        
                      
                        case 3:
                        { 
                            for(i=0; i<num1; i++)
                            {
                                printf("what is %d - %d\n",(one*2)-(i-3), one*=2) ;
                                scanf("%d", &answer3) ;
                            }
                            break ;
                        }
                        case 4:
                        {
                            for(i=0; i<num1; i++,four+=3)
                            {
                                printf("what is %d * %d\n", four, (four*2)-(i+3)) ;
                                scanf("%d", &answer4) ;
                            }
                            break ; 
                        }
                        
                    }
                   break ;
                }//end of case 2 of switch(option)
                
              
                case 3:                  //beginning of case 3. case 3 shows how many questions the user answer correctly
                {
                      if(option == 3 && num1 == 0)
                        {   
                            printf("You haven't answered question\n") ;
    
                            break ;
                        }
            
                    if(num1==1)
                    {
                        //by checking the various possibilities the user could have answered the question
                        if(one==2)
                        {
                            printf("i. You got 1 Correct!\n") ;
                        }
                        else
                        {
                            printf("ii. You got 1 Incorrect!!\n") ;
                        }
                        break ; 
                    }
                    if(num1==2) 
                    {
                        if(one == 2 && two == 9)
                        {
                            printf("i. You got 2 Correct!\n") ;
                            printf("ii. You got none Incorrect!!\n") ;
                        }
                        else if(one == 2 && two!= 9||one!=2 && two == 9)
                        {
                            printf("i. You got 1 Correct!\n") ;
                        }
                        else
                        {
                            printf("ii. You got Everything Incorrect!!\n") ;
                        }
                        break ;
                    }
                              
                    if(num1==3)
                    {
                        if(one != 2 && two !=9 && three == 24||one != 2 && two == 9 && three != 24||one == 2 && two != 9 && three != 24)
                        {
                            printf("i. You got 1 Correct!\n") ;
                            printf("ii. You got 2 Incorrect!!\n") ;
                           
                        }
                       
                            
                        else if(one == 2 && two == 9 && three != 24||one != 2 && two == 9 && three == 24||one == 2 && two != 9 && three == 24)
                        {
                            printf("i. You got 2 Correct!\n") ;
                            printf("ii. You got 1 Incorrect!!\n") ;
                           
                        }
                      
                        else if(one==2 && two==9 && three==24)
                        {
                            printf("i. You got Everything Correct\n") ;
                        }
                        else
                        {
                            printf("i. You got Everything Incorrect!!\n") ;
                        }
                        
                    }
                    
                    if(num1==4)
                    {
                        if(one == 2 && two !=9 && three != 24 && four != 100||one != 2 && two == 9 && three != 24 && four != 100||one != 2 && two != 9 && three == 24 && four!= 100||one != 2 && two != 9 && three != 24 && four==100)
                        {
                            printf("i.  You got 1 correct!\n") ;
                            printf("ii. You got 3 incorrect!!\n") ;
                           
                        }
                        
                        else if(one != 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four != 100|| one != 2 && two == 9 && three == 24 && four != 100|| one == 2 && two != 9 && three != 24 && four == 100)
                        {
                            printf("i. you got 2 correct\n") ;
                            printf("ii. you got 2 incorrect\n") ;
                            
                        }
                        else if(one != 2 && two == 9 && three == 24 && four == 100||one == 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four== 100||one == 2 && two == 9 && three == 24 && four!= 100)
                        {
                            printf("i. You got 3 Correct!\n") ;
                            printf("ii. You got 1 Incorrect!!\n") ;
                         
                        }
                        else if(one == 2 && two == 9 && three == 24 && four == 100)
                        {
                            printf("i. You got everything Correct\n") ;
                           
                        }
                        else
                        {
                            printf("i. you got everything Incorrect\n") ;
                        }
                        break;
                    }
                    break ;
                 
                }//end of switch(num1)
                
                case 4:
                { 
                    printf("Are you sure you want to exit quiz?\n") ;
                    printf("Y/N") ;
                    printf("\n") ;
                    scanf("%1s", &yesno) ;
                    if(yesno== 'Y'|| yesno == 'y')
                    {
                        exit(0) ;
                    }
                    clrscr() ;
                    break ; /*user must input y to quit*/
                }
            
                default: 
                {
                    printf("Can't you follow simple instructions?\n") ;
                    
                    break ; 
                }
                 
            }
        }while(option==1 || option==2 ||option==3 || option == 4) ;
        
        getch() ;
    }
    As you can see the code creates a semblance of randomness..i just have to figureout how to store the answers in a way that it won't overwrite the previous one..iwill look at ur example and see how to do it..and how to change option 3 as well..thanks man..pls tell me if option 2 is now better than before..
    yeah Borland C..is alright..but it is dimmer and makes my eyes sore..how didu get visual studio or C++ to compile for u? it doesnt work for me

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    lol sorry, i just like registered yday so i like looked everywhere for creating my own thread, counldn't find it lol.........but thanks, il post in c++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Having a class function call a function outside the class
    By Aeroren in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2005, 06:33 AM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 12:38 AM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21