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

Thread: Kindly Review My Code :-) Thanks in advance

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Kindly Review My Code :-) Thanks in advance

    Hi all ,
    i was solving one puzzle using JAVA , I have figured the solution , expected result is also coming but as My program is not up to the standard i am loosing points . Can anyone please help me figure it out what is not correct in my program . Thanks in advance .

    Problem Description :- There is one friendly number and N unfriendly numbers. We want to find how many numbers are there which exactly divide the friendly number, but does not divide any of the unfriendly numbers.

    Input Format:
    The first line of input contains two numbers N and K seperated by spaces. N is the number of unfriendly numbers, K is the friendly number.
    The second line of input contains N space separated unfriendly numbers.

    Output Format:
    Output the answer in a single line.

    Constraints:
    1 <= N <= 10^6
    1 <= K <= 10^13
    1 <= unfriendly numbers <= 10^18

    Sample Input:
    8 16
    2 5 7 4 3 8 3 18

    Sample Output:
    1

    Explanation :
    Divisors of the given friendly number 16, are { 1, 2, 4, 8, 16 } and the unfriendly numbers are {2, 5, 7, 4, 3, 8, 3, 18}. Now 1 divides all unfriendly numbers, 2 divide 2, 4 divide 4, 8 divide 8 but 16 divides none of them. So only one number exists which divide the friendly number but does not divide any of the unfriendly numbers. So the answer is 1.


    My Solution :- :-

    PHP Code:

    import java
    .util.Scanner;
    import java.util.Vector;


    public class 
    Solution {

        
        
    String[] input;
        
    Vector friendlyDivisor = new Vector();
        
    int[] unfriendlyNumbers;
        
    int noOfUnfriendlyNo;
        
    int friendlyNumber ;
        public static 
    void main(String[] args) {
            
            
            
    Solution classIns = new Solution();
            
    classIns.input = new String[2];
            
    Scanner scan = new Scanner(System.in);
            
            
    String getInput scan.nextLine();
            
    String[] tempArr getInput.split(" ");
            if(
    tempArr.length 2){
                
    System.exit(1);
            }
            if(
    classIns.validateInput(tempArr[0])){
                
    int temp1 Integer.parseInt(tempArr[0]);
                if(
    classIns.validateInput(temp1,"unfriendlyNumber")){
                    
    classIns.noOfUnfriendlyNo temp1;
                }
                else{
                    
    System.exit(1);
                }
                
            }
            else{
                
    System.exit(1);
            }
            if(
    classIns.validateInput(tempArr[1])){
                
    int temp2 Integer.parseInt(tempArr[1]);
                if(
    classIns.validateInput(temp2,"unfriendlyNumber")){
                    
    classIns.friendlyNumber temp2;
                    
    classIns.input[0] = getInput;
                }
                else{
                    
    System.exit(1);
                }
            }
            else{
                
    System.exit(1);
            }
            
    //System.out.println("Enter next line");
            
    String str scan.nextLine();
            
    classIns.input[1] = str;
            
    String[] tempArr2 str.split(" ");
            
    classIns.unfriendlyNumbers = new int[classIns.noOfUnfriendlyNo];
            if(
    classIns.unfriendlyNumbers.length != tempArr2.length){
                
    System.exit(1);
            }
            for(
    int count=;count<tempArr2.length;count++){
                
    String str2 tempArr2[count];
                if(
    classIns.validateInput(str2)){
                    
    int temp3 Integer.parseInt(str2);
                    if(
    classIns.validateInput(temp3,"unfriendlyNumberList")){
                        
    classIns.unfriendlyNumbers[count] = temp3;
                    }
                    else{
                        
    System.exit(1);
                    }
                }
                else{
                    
    System.exit(1);
                }
            }
            
    int result classIns.calculateSulution();
            
    System.out.println(result);
            
            
            
        }
        
        public 
    boolean validateInput(int num,String str){
            if(
    str.equalsIgnoreCase("friendlyNumber")){
                if(! ((
    num >= 1) &&(num<= Math.pow(10.013.0)))  ){
                    return 
    false;
                }
            }
            else if(
    str.equalsIgnoreCase("unfriendlyNumber")){
                if(! ((
    num >= 1) &&(num<= Math.pow(10.06.0)))  ){
                    return 
    false;
                }
            }
            else if(
    str.equalsIgnoreCase("unfriendlyNumberList")){
                if(! ((
    num >= 1) &&(num<= Math.pow(10.018.0)))  ){
                    return 
    false;
                }
            }
            return 
    true;
        }
        
        
        
        public 
    int calculateSulution(){
            
    int count 0;
            
    this.findDivisorForFriendlyNumbers();
            
    //for(int i = 0;i<this.friendlyDivisor.length;i++){
            
    for(Object o this.friendlyDivisor){
                
                
    int temp1 = (Integer)o;
                
    boolean test true;
                for(
    int j 0;j<this.unfriendlyNumbers.length;j++){
                    
    int temp2 this.unfriendlyNumbers[j];
                    if(
    temp2%temp1==0){
                        
    test false;
                        break;
                    }
                    if((
    == (this.unfriendlyNumbers.length 1)) && test){
                        
    count++;
                    }
                }
            }
            return 
    count;
        }
        
        
        public 
    void findDivisorForFriendlyNumbers(){
            
    int countDiv0;
            
    //Vector vect = new Vector();
            
    for(int count=1;count<=this.friendlyNumber;count++){
                if(
    this.friendlyNumber%count==0){
                    
    countDiv++;
                    
    this.friendlyDivisor.add(count);
                }
            }
            
            
        }
        
        public 
    boolean validateInput(String val){
            
    boolean result true ;
            try {

                
    Integer.parseInt(val);
            
            } catch (
    NumberFormatException ex) {
                return 
    false;
            }
            
            return 
    true;
            
        }


    Last edited by bhuppi; April 27th, 2012 at 09:39 AM. Reason: ..


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Kindly Review My Code :-) Thanks in advance

    There are a few things I would recommend:

    1. Use comments. These should help explain what your code is doing in a higher-level form.

    2. Don't use Vector. Use the ArrayList class instead. There are various reasons for this, but it boils down to ArrayList providing better performance and more control.

    3. Try to avoid using System.exit(). Personally I'm of the opinion that a program should terminate on it's own (i.e. return), not be force to terminate using System.exit().

    4. Consult the teaching staff for why they took points off. There may be something they're looking for or suggestions they could provide. Ultimately they're the ones in control of your grade, not us. Plus they probably looked over the program more thoroughly than I did.

Similar Threads

  1. Help with Clock (advance and reverse day,hour,minute)
    By whyld4900 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 13th, 2011, 02:39 AM
  2. Pls guide me in advance java
    By deepaks50 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 19th, 2011, 09:55 AM
  3. Help with looping etc THANKS ALOT IN ADVANCE
    By moesom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 15th, 2011, 11:43 AM
  4. Java coding for writing telephone bill calculator program
    By eyeore in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 8th, 2009, 11:06 AM
  5. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 01:55 PM