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

Thread: Understanding methods in my example code

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Understanding methods in my example code

    Hello everybody,

    I found some old practice material with questions. Almost all of the answers were in the document but nit all. I just want to make this documentation complete for myself and any other people who want to learn more about java.

    The code below is provided by the practice material and the following questions were not anywhere to be found in the documents. I have searched the internet to find resources and learn so I can answer the questions posted below the code.
    I hope you guys can help me to determine if the answers I have found through adapting the knowledge I found on the internet is right.

    package Practice;
    import javax.swing.JOptionPane;
     
    public class Oefening1{
     
    	public static void main(String[] args) {
    		char[] dader = {'A','A','C','A','G','G','C','T'};
     
    		char[] controle = new char[dader.length];
     
    		for(int x = 0;x < controle.length;x++){
    			controle[x] = leesletter();
    		}
     
    		int percentage = vergelijkDNA(dader,controle);
    		JOptionPane.showMessageDialog(null, "Overeenkomst " + percentage + " %",
    				"Uitvoer",JOptionPane.INFORMATION_MESSAGE);
    		System.exit(0);
    	}
     
    	public static char leesletter(){
    		char houder;
    		String temp = JOptionPane.showInputDialog(null,"Voer de letter A G C T in","CSI I",
    				JOptionPane.INFORMATION_MESSAGE);
    		houder = temp.charAt(0); //De eerste letter van de String wordt ingegeven.
     
    		if(houder == 'A'){
    			return houder;
    		}
    		else if(houder == 'G'){
    			return houder;
    		}
    		else if(houder == 'C'){
    			return houder;
    		}
    		else if(houder == 'T'){
    			return houder;
    		}
    		else {
    			return 'F';
    		}
    	}
     
    	public static int vergelijkDNA(char[] a,char[] b){
    		double dperc = 0;
    		double tel = 0;
    		int aantal = a.length;
     
    		for(int x = 0;x < a.length; x++){
    			if(a[x] == b[x]){
    				tel++;
    			}
    		}
     
    		dperc = tel/aantal;
    		System.out.println(tel + " " + aantal);
    		System.out.println(dperc);
    		double tperc = dperc * 100;
    		int perc = (int)tperc;
    		return perc;
    	}
    }

    Question 1: How many methods does this code contain?
    My answer:
    A. 8
    public static void main
    JOptionPane.showMessageDialog
    System.exit
    public static char leesletter (this method is called once in the main method)
    JOptionPane.showInputDialog
    public static int vergelijkDNA (this method is called once in the main method)
    System.out.println(tel + " " + aantal);
    System.out.println(dperc);

    Question 2: How many arrays are created in the main method?
    My answer: 2

    I hope to get some more insight in this matter.

    Thanks in advance!!!


  2. #2
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Can anybody please tell me if i am right on this?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Understanding methods in my example code

    How many methods does this code contain?
    Is that asking for the use of a method or the definition of a method?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Understanding methods in my example code

    charAt()?

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Understanding methods in my example code

    Quote Originally Posted by bodylojohn View Post
    Question 2: How many arrays are created in the main method?
    My answer: 2
    Not what I counted

  6. #6
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Understanding methods in my example code

    Quote Originally Posted by jps View Post
    Not what I counted
    Thank you very much jps..

    I forgot the String[] args in the main decleration.
    so its 3.

    I dont need to count the arrays that are created in the methods that are called in the main . Because those arrays are created in the specific method.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    charAt()?
    Thank you graigbrannon.
    I missed that one.
    So the total is 9?

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Understanding methods in my example code

    Quote Originally Posted by bodylojohn View Post
    So the total is 9?
    Hard to say for sure, you never responded to Norm's question...

  8. #8
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Understanding methods in my example code

    Quote Originally Posted by jps View Post
    Hard to say for sure, you never responded to Norm's question...
    Ow I am sorry... I quess I read over this one....

    The question in the practice material literally says this:
    How many methods does this code contain?
    And when I divide the question in:

    How many uses of methods?
    How many definitions of methods?

    Thanks a bunch so far guys. The more I am learning about java the more fun it is becoming!!!!

Similar Threads

  1. [SOLVED] Having problems understanding String Methods
    By kissyfurs in forum Java Theory & Questions
    Replies: 5
    Last Post: July 10th, 2013, 07:50 PM
  2. Need help understanding recrursive and static methods
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2012, 08:23 PM
  3. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 01:08 AM
  4. Need help understanding this code!
    By alex067 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2012, 01:14 AM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM