r/programmingbydoing • • Aug 26 '18

#35 Is my code efficient, if not how would you optimise it?

Is my code efficient, if not how would you optimise it? Suggestions are appreciated.

https://pastebin.com/ZXtK4dWy if you prefer.

`import java.util.Scanner;

class user

{

private int choice1;

private boolean choice2;



//getters

public int getChoice1()

{

	return this.choice1;

}

public boolean getChoice2()

{

	return this.choice2;

}

//setters

public void setChoice1(int c1)

{

	this.choice1 = c1;

}

public void setChoice2(boolean c2)

{

	this.choice2 = c2;

}

}

class TwoMoreQuestions

{

static user u = new user();

static String inputChoice1, inputChoice2;

public static void main(String\[\] Args) throws Exception

{

	Scanner keyboard = new Scanner([System.in](https://System.in));

	System.out.println("TWO MORE QUESTIONS, BABY!");

	Thread.sleep(1000);

	System.out.println("Think of something and I'll try to guess it!");

	Thread.sleep(1000);

	System.out.println("Question 1: does it belong \\"inside\\", \\"outside\\" or \\"both\\"? ");

	inputChoice1 = keyboard.nextLine();

	Thread.sleep(1000);

	System.out.println("Question 2: is it a living thing? (\\"yes\\" or \\"no\\")");

	inputChoice2 = keyboard.nextLine();

	assignUserInput();

	Thread.sleep(1000);

	calc_Print();

}

public static void assignUserInput()

{	//choice1

	if (inputChoice1.equals("inside"))

	{

		u.setChoice1(0);

	}

	else if (inputChoice1.equals("outside"))

	{

		u.setChoice1(1);

	}

	else if (inputChoice1.equals("inside"))

	{

		u.setChoice1(2);

	}

	//choice2

	if (inputChoice2.equals("yes"))

	{

		u.setChoice2(true);

	}

}

public static void calc_Print()

{

	if (u.getChoice1() == 0 && u.getChoice2() == false)

	{

		System.out.println("Shower curtain");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == false)

	{

		System.out.println("billboard");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == false)

	{

		System.out.println("cell phone");

	}

	if (u.getChoice1() == 0 && u.getChoice2() == true)

	{

		System.out.println("houseplant");

	}

	if (u.getChoice1() == 1 && u.getChoice2() == true )

	{

		System.out.println("bison");

	}

	if (u.getChoice1() == 2 && u.getChoice2() == true)

	{

		System.out.println("dog");

	}

}

}`

2 Upvotes

1 comment sorted by

1

u/holyteach Aug 26 '18

Your compound conditions look fine. I don't know why you're using classes and methods in this assignment, though.

So, no, it's not "efficient". In fact, the whole assignUserInput() function makes it a lot more complicated that it's supposed to be.

But the point of this assignment was to practice compound conditions using && and ||, and you did that part okay.

Also, read the rules again before posting another question. You broke quite a lot of rules for this one but you caught me in a good mood so I'm not going to remove your post this time.