So lets try to beat CloudCoder creating our version of a Com
So, let\'s try to beat CloudCoder creating our version of a \"Compiler\".
First tier (Grade from 0 to 15)
The software will accept in input a text file containing a Java Class. It will recognize the presence of Instance variables, Contructors and Methods. Plus, the package name and import packages.
If the file doesn\'t contain syntax errors, it will print in output something like \"the class is OK\".
If it contains errors, it will print the line numbers where the problems are.
Hints:
Any statement ends with a semicolon.
A class can belong to one package only and must be the first thing specified in a file (remember that is not mandatory).
Only one import with the same name can be considered correct. So, if the file is trying to import the same package twice, this is an error.
A class can contain only one Constructor with the same signature, but any with different signatures; also no Contructor at all.
The same with Methods, only one with the same signature, any with different signatures, or no Methods at all.
The Instance variables can be int, float, boolean, char and String.
Second tier (grade 16 to 20):
If everything from the previous tier works, the software will recognize multiple files in input. Each file will define a different class. Plus, the class now can contain an Object type different from String. This object can be an instance of the classes defined in the other files.
Hints:
To recognize an Object definition as legit, the software will check for the correct import statement. If the import is present, and the class exists, there will be no error. Example, if you have a Class file with the name \"Heroquest\" that belongs to the package \"tablegame\", it will check in the current file if an import to \"tablegame.Heroquest\" is present (or \"tablegame.*\" ) and if this file exists without errors.
Remember that, if you want to declare an instance of a Class, a Constructor is necessary in that Class. Otherwise, a default constructor will be generated automatically.
If the code is relying on a default constructor, an instance of the class can be created only if the constructor is used without specifying any parameter.
Third tier (grade 21 to 25):
If everything from the previous tiers is working fine, the software will also recognize inheritance (superclasses and subclasses), plus Interfaces and Abstract Classes.
Hints:
A subclass can inherit only from one superclass.
A Class can implement as many Interfaces it wants.
An Abstract Class cannot create instance of an object.
To implement an Interface the class have to define all the methods from that Interface.
Fourth Tier: (grade Extra Credits):
Considering that all the previous tiers are working fine, you can obtain Extra Credit increasing the number of functionalities.
More details will be given later on.
Solution
Please find below the program for your given task:
public class NewProgram {
public static void main (String[]args) throws IOException{
Scanner scan = renew Scanner(System.in);
int compareCount = 0;
int min = 0;
int max = 0;
int mid = 0;
int key = 0;
Scanner tempo;
int[]list;
String menu, outputString;
int option = 1;
boolean found = false;
// User will be prompted to select an option from the bemin
menu = \"\ \\t1 Reads file\" +
\"\ \\t2 Finds a specific no. in the list\" +
\"\ \\t3 Prints how many comparisons were needed\" +
\"\ \\t0 Exit\ \ \ \";
System.out.println(menu);
System.out.print(\"\\tPlease Enter your Choice: \");
option = scan.nextInt();
while (option != 0) {
switch (option) {
case 1:
readFile();
break;
case 2:
searchKey(list,min,max,key);
break;
case 3:
printCount();
break;
default: outputString = \"\ This is Invalid Choice\ \";
System.out.println(outputString);
break;
}
System.out.println(menu);
System.out.print(\"\\tEnter your Choice: \");
option = scan.nextInt();
} // End of while
} // End of main
public static void readFile() {
int i = 0;
tempo = new Scanner(new File(\"test.txt\"));
while(tempo.hasNext()) {
list[i]= tempo.nextInt();
i++;
}
tempo.close();
System.out.println(\"File Found...\");
}
public static void searchKey() {
while (found!=true) {
while (key < 50 || key > 500) {
System.out.println(\"Please enter the number you want to search for?\");
key = scan.nextInt();
}
if (min <= max) {
mid = ((min+max)/2);
if (key == list[mid]) {
found = true;
compareCount++;
}
}
else
if (key < list[mid]) {
compareCount++;
max = mid-1;
findKey(list,min,max,key);
}
else {
compareCount++;
min = mid+1;
searchKey(list,min,max,key);
}
}
}
public static void printCount() {
System.out.println(\"The Total nO. of comparisons is: \" + compareCount);
}
}


