In: Advanced Math
Implementation in CLIPS programming language for the following problem
Acme Electronics makes a device called the Thing 2000. This device is available in five different models distinguished by the chassis. Each chassis provides a number of bays for optional gizmos and is capable of generating a certain amount of power. The following table sumarizes the chassis attributes:
Chassis --------- Gizmo Bays provided --- Power Provided-----
Price($)
C100--------------------------- 1---------------------------
4------------------ 2000
C200------------------------- 2 --------------------------
5------------------ 2500
C300--------------------------- 3---------------------------7
------------------3000
C400---------------------------2----------------------------8------------------
3000
C500-------------------------- 4 ---------------------------9
------------------3500
Each gizmo that can be installed in the chassis requires a certain amount of power to operate. The following table summarizes the gizmo attributes
Gizmo-------------------- Power Used --------------------Price($)
Zaptron ------------------------ 2
------------------------------100
Yatmizer ----------------------- 6------------------------------
800
Phenerator--------------------- 1
------------------------------300
Malcifier----------------------- 3
------------------------------200
Zeta-shield------------------- 4
------------------------------150
Warnosynchronizer---------- 2
-------------------------------50
Dynoseparator---------------- 3
------------------------------400
Given as input facts representing the chassis and any gizmos that have been selected, write a program that generates facts representing the number of gizmos used, the total amount of power required for the gizmo, and the total price of the chassis and all gizmos selected
Answer:-
Here is a menu driven program that inputs the chasis type and the gizmaos and then calculates the total power and price.
If an invalid configuration (excessive power draw or insufficient gizmo bays) is detected then an appropriate error message is displayed.
import java.util.*;
class Acme {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int cost = 0;
int powerAvailable = 0;
int gizmosAvailable = 0;
int gizmosUsed = 0;
int powerUsed = 0;
while (true) {
System.out.println("Select a chassis: ");
System.out.println("\tName\tBays\tPower\tPrice\t");
System.out.println("1\tC100\t1\t4\t$2000");
System.out.println("2\tC200\t2\t5\t$2500");
System.out.println("3\tC300\t3\t7\t$3000");
System.out.println("4\tC400\t2\t8\t$3000");
System.out.println("5\tC500\t4\t9\t$3500");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
if (ch < 1 || ch > 5) {
System.out.println("Invalid selection");
continue;
} else {
switch (ch) {
case 1:
cost += 2000;
gizmosAvailable += 1;
powerAvailable += 4;
break;
case 2:
gizmosAvailable += 2;
powerAvailable += 5;
cost += 2500;
break;
case 3:
gizmosAvailable += 3;
powerAvailable += 7;
cost += 3000;
break;
case 4:
gizmosAvailable += 2;
powerAvailable += 8;
cost += 3000;
break;
case 5:
gizmosAvailable += 4;
powerAvailable += 9;
cost += 3500;
break;
}
break;
}
}
System.out.println("Enter your gizmos: ");
while (true) {
System.out.println("No. \tName\tPower\tPrice");
System.out.println("1\tZaptron\t2\t$100");
System.out.println("2\tYatmizer\t6\t$800");
System.out.println("3\tPhenerator\t1\t$300");
System.out.println("4\tMalcifer\t3\t$200");
System.out.println("5\tZeta-shield\t4\t$150");
System.out.println("6\tWarnosynchronizer\t2\t$50");
System.out.println("7\tDynoseparator\t3\t$400");
while (true) {
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
if (ch < 1 || ch > 7) {
System.out.println("Invalid choice");
continue;
} else {
switch (ch) {
case 1:
gizmosUsed++;
powerUsed += 2;
cost += 100;
break;
case 2:
gizmosUsed++;
powerUsed += 6;
cost += 800;
break;
case 3:
gizmosUsed++;
powerUsed += 1;
cost += 300;
break;
case 4:
gizmosUsed++;
powerUsed += 3;
cost += 200;
break;
case 5:
gizmosUsed++;
powerUsed += 4;
cost += 150;
break;
case 6:
gizmosUsed++;
powerUsed += 2;
cost += 50;
break;
case 7:
gizmosUsed++;
powerUsed += 3;
cost += 400;
break;
}
break;
}
}
System.out.print("Add another? (y/n) ");
sc.skip("\n");
String s = sc.nextLine();
if (s.equals("y")) continue;
else break;
}
if (gizmosUsed > gizmosAvailable) {
System.out.println("Invalid configuration!");
System.out.println("Not enough gizmo slots!");
return;
}
if (powerUsed > powerAvailable) {
System.out.println("Invalid configuration!");
System.out.println("Not enough power!");
return;
}
System.out.printf("Total number of gizmos: %d\n",
gizmosUsed);
System.out.printf("Total power used: %d\n", powerUsed);
System.out.printf("Total cost: $%d.00\n", cost);
}
}