Professional Documents
Culture Documents
PROJECT
By:
Adlawan, Dominic Bien
Decamotan, Edison
Galceran, Quint
import java.util.*;
public class FeedDoggy
{
public static Scanner c= new Scanner (System.in);
int rooms;
System.out.print("\nEnter number of rooms: ");
rooms= c.nextInt();
//FOR DOG
String dog; int d; int dd; int valid=1; int dogcount=0; //valid checks user's input: 1 for correct and 0
for wrong inputs
System.out.println("\nMark with '2' the room where the DOG is initially at, '0' for the two other
rooms:");
dog= c.next();
if (dog.length()<(rooms) || dog.length()>rooms ){ //if user inputs less/ more than no. of rooms,
program terminates
System.out.println("Input lacks/ exceeds, re-run program!");
return;
}
for (d=0; d<dog.length(); d++){ //checks if user inputs 2's and 0's only
if (dog.charAt(d)!='2' && dog.charAt(d)!='0'){
valid=0;
}
}
if (valid==0){ //if user inputs other than 2's and 0's, program terminates
System.out.println("Input must be 2's and 0's only, re-run program!");
return;
}
for (dd=0; dd<dog.length(); dd++){ //counta no. of dogs the user inputs
if (dog.charAt(dd)=='2'){
dogcount++;
}
}
//FOR AGENT
String agent; int a, aa; int valid1= 1; int agentcount=0;
System.out.println("Mark '1' the room where the AGENT is initially at, '0' for the other rooms: ");
agent= c.next();
for (a=0; a<agent.length(); a++){ //checks if user inputs 1's and 0's only
if (agent.charAt(a)!='1' && agent.charAt(a)!='0'){
valid1=0;
}
}
if (valid1==0){ //if user inputs other than 1's and 0's, program terminates
System.out.println("Input must be 1's and 0's only, re-run program!");
return;
}
if (dogcount>1 || dogcount<1 ){ //if user has less/ more than dogs and agents, program
terminates
System.out.println("\nONLY 1 DOG AND 1 AGENT SHOULD BE IN THE LOCATION. PLEASE RUN
THE PROGRAM AGAIN.");
return;
}else if( agentcount>1 || agentcount<1){
System.out.println("\nONLY 1 DOG AND 1 AGENT SHOULD BE IN THE LOCATION. PLEASE RUN
THE PROGRAM AGAIN.");
return;
}
else{
System.out.println("\nOnly 1 Dog and 1 Agent found.");
}
for (ddd=0; ddd<rooms; ddd++){ //finds the room of dog and agent
if (dog.charAt(ddd)=='2'){
break;
}
}
for (aaa=0; aaa<rooms; aaa++){
if (agent.charAt(aaa)=='1'){
break;
}
}
int room; int found; int cont=1; //cont checks if dog and agent arent in same room yet, reads the
next lines below
for (room=0; room<rooms; room++){
if (ddd==room && aaa==room){ //if in the same room already, prints "found" statement and
terminates the program.
System.out.print("\t");
for (found=0; found<rooms; found++){
if (ddd==found){
System.out.print(3);
}else{
System.out.print("0");
}
}
System.out.print("\n");
System.out.println("\nAGENT ALREADY FOUND THE DOG. DOG FED!");
//if dog and agent are in the same room the value of cont will set to 0
cont=0;
break;
}
}
for (room1=0; room1<rooms; room1++){ //prints no. of room the dog and agent are
if (ddd==room1){
System.out.print(dog.charAt(ddd));
}else if (aaa==room1){
System.out.print(agent.charAt(aaa));
}else {
System.out.print("0");
}
}
System.out.println("\n");
break;
}
}
}
}