-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.java
More file actions
119 lines (110 loc) · 3.47 KB
/
Inheritance.java
File metadata and controls
119 lines (110 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.*;
import java.util.*;
class Memory{
protected static boolean[] occupied = new boolean[100];
protected String decimalToHex(int num){
// For storing remainder
int rem;
// For storing result
String str2="";
// Digits in hexadecimal number system
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//Converting decimal to hexadecimal
while(num>0){
rem=num%16;
str2=hex[rem]+str2;
num=num/16;
}
//Returning the result
return str2;
}
}
class Calculations extends Memory{
public Scanner wats = new Scanner(System.in);
//Function to set Memory
public void setMemory(){
System.out.println("Enter your choice");
System.out.println("1. String");
System.out.println("2. Integer");
int choice = wats.nextInt();
int freePos = 0;
switch (choice) {
case 1:
setString();
break;
case 2 :
setInt();
break;
default:
break;
}
}
//Function to set string
void setString(){
System.out.println("Enter the position");
int pos = wats.nextInt();
System.out.println("Provide your String");
String str = wats.next();
int length = str.length();
int freePos = -1;
freePos = isString(pos , length);
if(freePos == 0)
System.out.println("Allocation Done");
else
System.out.println("Memory is not free , Next free space is at location " + decimalToHex(freePos));
}
//Function to set int
void setInt(){
System.out.println("Enter the position");
int pos = wats.nextInt();
System.out.println("Enter you integer");
int data = wats.nextInt();
int freePos = -1;
freePos = isInt(pos);
if(freePos == 0)
System.out.println("Allocation Done");
else
System.out.println("Memory is not free , Next free space is at location " + decimalToHex(freePos));
}
//Function to tell if the given integer can be allocated the given Memory
int isInt(int pos){
int flag = 0;
for(int i = pos ; i < pos + 4 ; i++)
if(occupied[i] == true)
flag = 1;
if(flag == 0){
for(int i = pos ; i < pos + 4 ; i ++)
occupied[i] = true;
return flag;
}
for(int i = pos ; i < 100 - pos ; i++)
if(occupied[i] == false)
return i;
System.out.println("yes");
return 0;
}
//Function to tell if the given string can be alloted the given memory
int isString(int pos , int length){
int flag = 0;
for(int i = pos ; i < pos + 2*length ; i++)
if(occupied[i] == true)
flag = 1;
if(flag == 0){
for(int i = pos ; i < pos + 2*length ; i++)
occupied[i] = true;
return flag;
}
for(int i = pos ; i < 100 - pos ; i++)
if(occupied[i] == false)
return i;
return 0;
}
}
class Inheritance extends Calculations{
public static void main(String args[])throws IOException{
Inheritance obj1 = new Inheritance();
Inheritance obj2 = new Inheritance();
obj1.setMemory();
obj2.setMemory();
}
}