Skip to content

Commit a0d3c70

Browse files
committed
Document (IPA-35)
1 parent 5135e1b commit a0d3c70

2 files changed

Lines changed: 181 additions & 116 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Create class Document with below attributes
2+
3+
id - int
4+
title - String
5+
folderName - String
6+
pages - int
7+
8+
Write getters, setters and parameterized constructor as required.
9+
10+
Create class Solution with main method.
11+
12+
Implement static method - docsWithOddPages in Solution class.
13+
14+
This method will take array of Document objects and return another array with Document objects which has odd number of pages.
15+
16+
This method should be called from main method and display values of returned objects as shared in the sample (in ascending
17+
order of id attribute).
18+
19+
Before calling this method, use Scanner object to read values for four Document objects referring attributes in the above
20+
sequence.
21+
22+
Next call the method and display the result.
23+
24+
Consider below sample input and output:
25+
26+
Input:
27+
1
28+
resume
29+
personal
30+
50
31+
2
32+
question1
33+
exams
34+
55
35+
3
36+
question2
37+
exams
38+
45
39+
4
40+
India
41+
misc
42+
40
43+
44+
45+
Output (each line has values separated by single space):
46+
2 question1 exams 55
47+
3 question2 exams 45
48+
49+
50+
51+
Note on using Scanner object:
52+
Sometimes scanner does not read the new line character while invoking methods like nextInt(), nextDouble() etc.
53+
Usually, this is not an issue, but this may be visible while calling nextLine() immediately after those methods.
54+
55+
Consider below input values:
56+
22
57+
hello
58+
59+
Referring below code:
60+
61+
Scanner sc = new Scanner(System.in);
62+
int x = sc.nextInt();
63+
String str = sc.nextLine(); -> here we expect str to have value hello. Instead it may be """".
64+
65+
If above issue is observed, then it is suggested to add one more explicit call to nextLine() after reading numeric value.

DocsWithOddPagesByAssId.java renamed to Document With Odd Pages/DocsWithOddPagesByAssId.java

Lines changed: 116 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,116 @@
1-
package JAVA;
2-
import java.io.*;
3-
import java.util.*;
4-
import java.text.*;
5-
import java.math.*;
6-
import java.util.regex.*;
7-
8-
public class DocsWithOddPagesByAssId {
9-
public static void main(String args[] ) throws Exception {
10-
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
11-
Scanner sc = new Scanner(System.in);
12-
Document[] doc = new Document[4];
13-
for(int i =0; i<4; i++)
14-
{
15-
int a = sc.nextInt();sc.nextLine();
16-
String b = sc.nextLine();
17-
String c = sc.nextLine();
18-
int d = sc.nextInt();
19-
20-
doc[i] = new Document(a,b,c,d);
21-
}
22-
Document[] d = docWithOddPages(doc);
23-
if(d==null)
24-
{
25-
System.out.println("All pages are even");
26-
}
27-
else
28-
{
29-
for(int i =0; i<d.length; i++)
30-
{
31-
System.out.println(d[i].getId()+" "+d[i].getTitle()+" "+d[i].getFolderName()+" "+d[i].getPages());
32-
}
33-
}
34-
35-
}
36-
public static Document[] docWithOddPages(Document[] d)
37-
{
38-
Document[] st = new Document[0];
39-
for(int i=0; i<d.length; i++)
40-
{
41-
if(d[i].pages%2!=0)
42-
{
43-
st = Arrays.copyOf(st,st.length+1);
44-
st[st.length-1]=d[i];
45-
}
46-
}
47-
for(int i =0; i<st.length; i++)
48-
{
49-
for(int j=i+1; j<st.length; j++)
50-
{
51-
if(st[i].getId()>st[j].getId())
52-
{
53-
Document k=st[i];
54-
st[i]=st[j];
55-
st[j]=k;
56-
}
57-
}
58-
}
59-
if(st.length>0)
60-
{
61-
return st;
62-
}
63-
else
64-
{
65-
return null;
66-
}
67-
}
68-
}
69-
class Document
70-
{
71-
int id;
72-
String title;
73-
String folderName;
74-
int pages;
75-
76-
public Document(int id, String title, String folderName, int pages)
77-
{
78-
this.id = id;
79-
this.title = title;
80-
this.folderName = folderName;
81-
this.pages = pages;
82-
}
83-
84-
public int getId()
85-
{
86-
return id;
87-
}
88-
public void setId(int id)
89-
{
90-
this.id = id;
91-
}
92-
public String getTitle()
93-
{
94-
return title;
95-
}
96-
public void setTitle(String title)
97-
{
98-
this.title = title;
99-
}
100-
public String getFolderName()
101-
{
102-
return folderName;
103-
}
104-
public void setFolderName(String folderName)
105-
{
106-
this.folderName = folderName;
107-
}
108-
public int getPages()
109-
{
110-
return pages;
111-
}
112-
public void setPages(int pages)
113-
{
114-
this.pages = pages;
115-
}
116-
}
1+
2+
import java.io.*;
3+
import java.util.*;
4+
import java.text.*;
5+
import java.math.*;
6+
import java.util.regex.*;
7+
8+
public class DocsWithOddPagesByAssId {
9+
public static void main(String args[] ) throws Exception {
10+
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
11+
Scanner sc = new Scanner(System.in);
12+
Document[] doc = new Document[4];
13+
for(int i =0; i<4; i++)
14+
{
15+
int a = sc.nextInt();sc.nextLine();
16+
String b = sc.nextLine();
17+
String c = sc.nextLine();
18+
int d = sc.nextInt();
19+
20+
doc[i] = new Document(a,b,c,d);
21+
}
22+
Document[] d = docWithOddPages(doc);
23+
if(d==null)
24+
{
25+
System.out.println("All pages are even");
26+
}
27+
else
28+
{
29+
for(int i =0; i<d.length; i++)
30+
{
31+
System.out.println(d[i].getId()+" "+d[i].getTitle()+" "+d[i].getFolderName()+" "+d[i].getPages());
32+
}
33+
}
34+
35+
}
36+
public static Document[] docWithOddPages(Document[] d)
37+
{
38+
Document[] st = new Document[0];
39+
for(int i=0; i<d.length; i++)
40+
{
41+
if(d[i].pages%2!=0)
42+
{
43+
st = Arrays.copyOf(st,st.length+1);
44+
st[st.length-1]=d[i];
45+
}
46+
}
47+
for(int i =0; i<st.length; i++)
48+
{
49+
for(int j=i+1; j<st.length; j++)
50+
{
51+
if(st[i].getId()>st[j].getId())
52+
{
53+
Document k=st[i];
54+
st[i]=st[j];
55+
st[j]=k;
56+
}
57+
}
58+
}
59+
if(st.length>0)
60+
{
61+
return st;
62+
}
63+
else
64+
{
65+
return null;
66+
}
67+
}
68+
}
69+
class Document
70+
{
71+
int id;
72+
String title;
73+
String folderName;
74+
int pages;
75+
76+
public Document(int id, String title, String folderName, int pages)
77+
{
78+
this.id = id;
79+
this.title = title;
80+
this.folderName = folderName;
81+
this.pages = pages;
82+
}
83+
84+
public int getId()
85+
{
86+
return id;
87+
}
88+
public void setId(int id)
89+
{
90+
this.id = id;
91+
}
92+
public String getTitle()
93+
{
94+
return title;
95+
}
96+
public void setTitle(String title)
97+
{
98+
this.title = title;
99+
}
100+
public String getFolderName()
101+
{
102+
return folderName;
103+
}
104+
public void setFolderName(String folderName)
105+
{
106+
this.folderName = folderName;
107+
}
108+
public int getPages()
109+
{
110+
return pages;
111+
}
112+
public void setPages(int pages)
113+
{
114+
this.pages = pages;
115+
}
116+
}

0 commit comments

Comments
 (0)