Class Ascending - ISC COMPUTER PROJECT 2018: Import Import Class Int

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Class Ascending - ISC COMPUTER PROJECT 2018 1/2

1 import java.io.*;
2 import java.util.Scanner;
3
4 class Ascending
5 {
6 int a1[], size;
7
8 public Ascending(){}
9
10 public Ascending (int n) throws IOException
11 {
12 Scanner sc=new Scanner (System.in);
13 int i, value;
14 size=n;
15 a1=new int[size];
16
17 for(i=0; i<=n-1; i++)
18 {
19 value=sc.nextInt();
20 a1[i]=value;
21 }
22 System.out.println("Input taking ends");
23 }
24
25 public void displayList()
26 {
27 int i;
28 for (i=0; i<=size-1; i++)
29 {
30 System.out.print( a1[i] + " ");
31 }
32 }
33
34 Ascending merge (Ascending as) throws Exception
35 {
36 Ascending mergedObject=new Ascending();
37 int fi=0, si=0, ti=0; /* fi first index for calling o
bject; si second index for object passed as argument; ti third ind
ex for object to be created */
38 int i;
39 mergedObject.a1=new int [this.size+as.size];
40 //mergedObject.size= this.size + as.size;
41
42 while (fi<=this.size-1 && si<=as.size-1)
43 {
44
45 if (this.a1[fi] < as.a1[si] )
46 {
47 if ( ti>0 && this.a1[fi] == mergedObject.a1[ti-1]
) // checking if the same value already transferred to mergedObje
ct
48 fi++;
49 else

5 Aug, 2017 10:06:51 PM


Class Ascending - ISC COMPUTER PROJECT 2018 (continued) 2/2

50 {
51 mergedObject.a1[ti]= this.a1[fi];
52 fi++; ti++;
53 }
54 }
55 else
56 {
57 if( ti>0 && as.a1[si] == mergedObject.a1[ti-1] )
58 si++;
59 else
60 {
61 mergedObject.a1[ti]=as.a1[si];
62 si++; ti++;
63 }
64 }
65 }
66
67 if( fi==this.size) /* indicated all elements of calling ob
ject shifted to mergedObject */
68 {
69 for (i=si; i<=as.size-1; i++)
70 {
71 if( as.a1[i] != mergedObject.a1[ti-1] )
72 mergedObject.a1[ti++]= as.a1[i];
73 }
74 }
75 else
76 if (si==as.size)
77 {
78 for(i=fi; i<=this.size-1; i++)
79 {
80 if( this.a1[i] != mergedObject.a1[ti-1] )
81 mergedObject.a1[ti++]=this.a1[i];
82 }
83 }
84
85 mergedObject.size=ti;
86 return mergedObject;
87 }
88
89 public static void main(String arg[]) throws Exception
90 {
91 Ascending asc=new Ascending(4);
92 Ascending asc1=new Ascending(4);
93
94 Ascending ascMerged= asc.merge(asc1);
95
96 ascMerged.displayList();
97 }
98 }
99

5 Aug, 2017 10:06:52 PM

You might also like