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

Thread: Convert to OOP approach

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Convert to OOP approach

    Hi guys, please help me to convert this code to OOP approach that that contains a method.
    I hope someone will response . Thanks in advance

    import java.io.*;
    public class Project
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader br=new BufferedReader(
                        new InputStreamReader(System.in));
                    int ar[]=new int[100];
                    int n,i,j,pos,ch;
                    System.out.print("Enter number of element : ");
                    n=Integer.parseInt(br.readLine());
                    if(n<=99)
                        {
                            for(i=0;i<n;i++)
                                {
                                    System.out.print("Enter any number : ");
                                    ar[i]=Integer.parseInt(br.readLine());
                                }
                            do
                                {
                                    System.out.println("1.Insert Element");
                                    System.out.println("2.Delete Element");
                                    System.out.println("3.Display Element");
                                    System.out.println("4.Exit");
                                    System.out.print("Enter your choice : ");
                                    ch=Integer.parseInt(br.readLine());
                                    switch(ch)
                                        {
                                            case 1:
                                                System.out.print("Enter position number : ");
                                                pos=Integer.parseInt(br.readLine())-1;
                                                if(pos<=n)
                                                    {
                                                        for(j=n-1;j>=pos;j--)
                                                            ar[j+1]=ar[j];
                                                        System.out.print("Insert a new Element : ");
                                                        ar[j+1]=Integer.parseInt(br.readLine());
                                                        n++;
                                                    }
                                                else
                                                    System.out.println("Invalid Position");
                                                break;
                                            case 2:
                                                System.out.print("Enter position number : ");
                                                pos=Integer.parseInt(br.readLine())-1;
                                                if(pos<=n)
                                                    {
                                                        for(j=pos;j<n;j++)
                                                            ar[j]=ar[j+1];
                                                        System.out.println("Element Deleted");
                                                        n--;
                                                    }
                                                else
                                                    System.out.println("Invalid Position");
                                                break;
                                            case 3:
                                                for(i=0;i<n;i++)
                                                    System.out.print(ar[i]+"\t");
                                                System.out.println();
                                                break;
                                            case 4:
                                                System.out.println("Thank You!");
                                                System.exit(0);
                                                break;
                                            default:
                                                System.out.println(ch + " is not available at the choices");
                                                System.out.println("Please enter again!");
                                        }
                                }while(ch != 4 );
                        }
                    else
                        System.out.println("Out of bound!");
     
                }
        }


  2. #2
    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: Convert to OOP approach

    Looks like homework to me.
    Why don't you try it and see what you can come up with, or explain what problem you are facing

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert to OOP approach

    I don't know how .I'm a new in java

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Convert to OOP approach

    I don't know how to cook. Will you come over to my place every night and cook me dinner?

    I don't know how to play a musical instrument. After you have cooked my dinner will you play the guitar so I have some nice music to eat by?

    Or maybe I could learn!
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Convert to OOP approach

    I'm finish to convert it to OOP approach .. but I have an other problem .. when I choice 2.Delete the program will exit.
    Please tell me what to do ..
    here's the code
    import java.util.Scanner;
    public class Project
    {
        private int n = 0;
        private int pos = 0;
        private int ar[]=new int[100];
     
        public void setNum(int n){
            this.n = n;
        }
     
        public void setPos(int pos){
            this.pos = pos;
        }
     
        public int getPos(){
            return pos;
        }
     
        public int getNum(){
            Scanner br = new Scanner(System.in);
     
            if(n<=99){
                for(int i=0;i<n;i++){
                     System.out.print("Enter any number : ");
                     ar[i]=Integer.parseInt(br.nextLine());
                }                 
            }
            else
                System.out.print("Out of Bound!!");
            return n;
        }
     
        public void Insert(){
             Scanner br = new Scanner(System.in);
            int j;
            if(pos<=n)
            {
                for( j=n-1;j>=pos;j--)
                    ar[j+1]=ar[j];
                System.out.print("Insert a new Element : ");
                ar[j+1]=Integer.parseInt(br.nextLine());
                n++;
     
            }
        else
            System.out.println("Invalid Position");
        }
     
        public void Delete(){
            Scanner br = new Scanner(System.in);
            int j;
            if(pos<=n)
            {
                for(j=pos;j<n;j++)
                ar[j]=ar[j+1];
                System.out.println("Element Deleted");
                ar[j]=Integer.parseInt(br.nextLine());
                n--;
            }
     
        else
            System.out.println("Invalid Position");
        }
     
        public void Display(){
            for(int i=0;i<n;i++)
            System.out.print(ar[i] + "\t");
        }
        public static void main(String args[])
            {
                Scanner br = new Scanner(System.in);
     
                int n,pos,ch;
                Project a = new Project();
                System.out.print("Enter number of element : ");
                n=Integer.parseInt(br.nextLine());
                a.setNum(n);
                a.getNum();
                        do
                            {
                                System.out.println("1.Insert Element");
                                System.out.println("2.Delete Element");
                                System.out.println("3.Display Element");
                                System.out.println("4.Exit");
                                System.out.print("Enter your choice : ");
                                ch=Integer.parseInt(br.nextLine());
                                switch(ch)
                                    {
                                        case 1:
                                            System.out.print("Enter position number : ");
                                            pos=Integer.parseInt(br.nextLine())-1;
                                            a.setPos(pos);
                                            a.Insert();
                                            break;
                                        case 2:
                                            System.out.print("Enter position number : ");
                                            pos=Integer.parseInt(br.nextLine())-1;
                                            a.setPos(pos);
                                            a.Delete();
                                            break;
                                        case 3:
                                            a.Display();
                                            System.out.println();
                                            break;
                                        case 4:
                                            System.out.println("Thank You!");
                                            System.exit(0);
                                            break;
                                        default:
                                            System.out.println(ch + " is not available at the choices");
                                            System.out.println("Please enter again!");
                                    }
                            }while(ch != 4 );
     
     
            }
    }

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Convert to OOP approach

    What is that line supposed to do in Delete?
    ar[j]=Integer.parseInt(br.nextLine());
    Other tings:
    - Stick to the coding conventions, method names are lower case.
    - shorten your main method, it should not contain more than a few lines of initialization code
    - use one method to get input as an Integer
    - make sure the user enters only Integers
    - use more than one class, your dynamic Array should be a seperate class.
    - never use System.exit(), it kills the VM not only your program

  7. The Following User Says Thank You to PhHein For This Useful Post:

    driczdc (August 28th, 2013)

Similar Threads

  1. how to approach oops
    By veera in forum Java Theory & Questions
    Replies: 2
    Last Post: August 1st, 2013, 02:23 PM
  2. What approach should I use?
    By techflyer in forum Java Theory & Questions
    Replies: 2
    Last Post: June 9th, 2013, 07:59 PM
  3. How would I approach this?
    By thatguywhoprograms in forum Java Theory & Questions
    Replies: 5
    Last Post: December 20th, 2011, 10:08 PM
  4. How would you approach this?
    By Staticity in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 12:09 AM
  5. How best to approach this project?
    By eb_dev in forum Java Theory & Questions
    Replies: 0
    Last Post: March 22nd, 2011, 11:43 AM