# Convert to OOP approach

• August 28th, 2013, 12:19 AM
driczdc
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

Code :

```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!");   } }```
• August 28th, 2013, 12:40 AM
jps
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
• August 28th, 2013, 12:54 AM
driczdc
Re: Convert to OOP approach
I don't know how .I'm a new in java
• August 28th, 2013, 02:04 AM
Junky
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!
• August 28th, 2013, 04:36 AM
driczdc
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
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 );     } }```
• August 28th, 2013, 06:50 AM
PhHein
Re: Convert to OOP approach
What is that line supposed to do in Delete?
Code :

`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