Download 1 - JustAnswer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

Types of artificial neural networks wikipedia , lookup

Delta-sigma modulation wikipedia , lookup

Nonblocking minimal spanning switch wikipedia , lookup

Pattern recognition wikipedia , lookup

SahysMod wikipedia , lookup

Planted motif search wikipedia , lookup

Transcript
1.Problem statement
Develop a Program that will read the age from user input and store it in to a Integer Bag Data
Structure. In addition that program should check the whether size of Bag exceeds the Limit.
When User Inputs -1 program should leave the age input mode . then it should display following
options
a) print all the ages of your family members
b) output number of occurrences of given age
c) remove age given in the input.
d) Exit the System.
Enter a,b,c or d ->
According to user selection program should display the output.
2. Design methodology
Object Oriented Approach is used to develop the software
Class Diagramm
3. Program code
public class IntegerBag {
private int capacity, index;
private int[] bagArray;
public IntegerBag() {
}
public int getCapacity() {
return capacity;
}
public IntegerBag(int capacity) {
this.capacity = capacity;
bagArray = new int[0];
}
public boolean isExists(int value) {
for (int i : getBagArray()) {
if (i == value) {
return true;
}
}
return false;
}
public boolean isFull() {
return getBagArray().length == capacity;
}
public int findoccurrences(int val) {
int count = 0;
for (int i : getBagArray()) {
if (i == val) {
count++;
}
}
return count;
}
public void remove(int val) {
int ofset = 0, count = 0;
count = findoccurrences(val);
int tmp[] = new int[bagArray.length - count];
for (int i = 0; i < bagArray.length; i++) {
if (bagArray[i] != val) {
tmp[i - ofset] = bagArray[i];
} else {
ofset++;
}
}
bagArray = tmp;
}
public void add(int value) {
if (bagArray.length == 0) {
bagArray = new int[1];
bagArray[0] = value;
} else {
if (!isFull()) {
int tmp[] = new int[getBagArray().length + 1];
for (int i = 0; i < tmp.length - 1; i++) {
tmp[i] = getBagArray()[i];
}
tmp[bagArray.length] = value;
bagArray = tmp;
}
}
}
public int[] getBagArray() {
return bagArray;
}
}
import java.io.*;
public class FamilyAdder {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bfr = new BufferedReader(isr);
IntegerBag familyAgeBag = new IntegerBag(10);
int input = 0;
while (!(input < 0)) {
try {
if (!familyAgeBag.isFull()) {
System.out.println("Enter the age of your next family member or -1 to terminate:");
input = Integer.valueOf(bfr.readLine());
if (input > 0) {
familyAgeBag.add(input);
}
} else {
System.out.println("Bag is full, Enter -1 to terminate:");
input = Integer.valueOf(bfr.readLine());
}
} catch (Exception exp) {
System.err.println("Invalid Input.");
}
}
String inp;
while (true) {
System.out.println("a) print all the ages of your family members ");
System.out.println("b) output number of occurrences of given age ");
System.out.println("c) remove age given in the input.");
System.out.println("d) Exit the System");
System.out.print("Enter a,b,c or d -> ");
try {
inp = bfr.readLine();
if (inp.equalsIgnoreCase("a")) {
printAges(familyAgeBag);
} else if (inp.equalsIgnoreCase("b")) {
int age;
System.out.println("Enter the age that you want to find.");
age = Integer.valueOf(bfr.readLine());
System.out.println("Age " + age + " Found " + familyAgeBag.findoccurrences(age)
+ " Times");
} else if (inp.equalsIgnoreCase("c")) {
System.out.println("Enter the age that you want to remove.");
int age;
age = Integer.valueOf(bfr.readLine());
familyAgeBag.remove(age);
printAges(familyAgeBag);
} else if (inp.equalsIgnoreCase("d")) {
System.out.println("Exiting.");
break;
} else {
}
} catch (Exception exp) {
}
}
}
private static void printAges(IntegerBag familyAgeBag) {
System.out.println("Printintg the Complete List Of Age.");
for (int i : familyAgeBag.getBagArray()) {
System.out.println(i);
}
}
}
4. Results (including compiler output)
5. Concluding remarks
It’s a Great Experience to develop this Program.