CS1020 HelloWorld Lab 0 Ex 1

As I had recently completed my CS1020 Data Structures and Algorithms I module at School of Computing in National University of Singapore, I decided to share some of my code used in the module. As there are quite a few set of questions, I will spread the code in the next few posts. The code posted here had been tested against codecrunch (SoC Code submission system) however it doesn’t mean that it is the most optimized code. Do leave any improvement in the comment. As the question is could be quite lengthy, therefore I will upload the question in the form of pdf.

Link to the question – HelloWorld Lab 0 Ex 1

import java.util.*;

public class HelloWorld {
	
	public static void main(String[] args) {

        // declare the necessary variables


		// create new object from class Result


		// declare a Scanner object to read input

		int result = 0;
		// read input and process them accordingly
		Scanner scan = new Scanner(System.in);
		int inputType = scan.nextInt(); //assume it is always int
		List<String> arrOperation = new ArrayList<String>();
		switch(inputType)
		{
			case 1:
				int numberOfOperation = scan.nextInt(); //assume it is always int

				for(int i = 0; i < numberOfOperation; i++)
				{
					String operation = scan.next(); //assume it is always AND/OR and 0/1
					int operand1 = scan.nextInt();
					int operand2 = scan.nextInt();
					result = processOperation(operation, operand1, operand2);
					arrOperation.add(Integer.toString(result));
				}
				break;
			case 2:
				boolean exit = false;
				do
				{
					String operation = scan.next();
					if(!operation.equals("0"))
					{
						int operand1 = scan.nextInt();
						int operand2 = scan.nextInt();
						result = processOperation(operation, operand1, operand2);
						arrOperation.add(Integer.toString(result));
					}
					else
					{
						exit = true;
					}
				}while(exit == false);
				break;
			case 3:
				while(scan.hasNext())
				{
					String operation = scan.next();
					int operand1 = scan.nextInt();
					int operand2 = scan.nextInt();
					result = processOperation(operation, operand1, operand2);
					arrOperation.add(Integer.toString(result));
				}
				break;
		}
		for(String i : arrOperation)
		{		
			System.out.println(i);
		}
	}

	static int processOperation(String operation, int operand1, int operand2)
	{
		int result = 0;
		if(operation.equals("AND"))
		{
			result = operand1 & operand2;
		}
		else if(operation.equals("OR"))
		{
			result = operand1 | operand2;
		}
		return result;
	}
}
Advertisements
This entry was posted in NUS, SoC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s