Jump to content

so I've got a code that I've started in a program called BlueJ, a work environment used for java. The program I am trying to write is basically a wizard school quiz to find out which school you belong to. If you need to get an Idea if what im trying to make, Im basically trying to replicate the intro quiz from wizard101, which can be found here: https://www.wizard101.com/create_wizard/?utm_campaign=disp_ad2games&utm_source=ad2games.18470_3278p&utm_medium=display&utm_content=wizard

 

If anyone could help me out, I just need help figuring out how to set up basic choices kind of like an A,B,C,D sort of quiz. I just want to learn the fundamentals of writing a small quiz. 

 

I am a few weeks into an AP Computer Science class and I just sort of want to learn this concept a little before it is taught.

 

Here is my code so far:

59d6e08f2d156_DesktopScreenshot2017_10.05-20_40_58_14.thumb.png.7b52c6f1918f90e00c455e2107d0ced5.png

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to comment
https://linustechtips.com/topic/843033-anyone-wanna-help-me-with-this-java-code/
Share on other sites

Link to post
Share on other sites

Just now, tjcater said:

I'm not that well versed in Java but don't you need to declare the variable as a sting/float/etc else you get an error?

Alright, I might try changing the order of how things are declared

 

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to post
Share on other sites

1 minute ago, tjcater said:

Not the order but declaring it like:


Float someVariable = School.Fire
String someVariable = School.Something

 

Oh that makes sense, alright I'll try that

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to post
Share on other sites

you could use a Scanner to take user input, and specify in the question it must be "a, b, c, d"

sort of like this:

String ans;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please answer a, b, c, or d");
System.out.println("What is your favorite type of aardvark? \n a. eastern \n b. western \n c. northern \n d. midwestern");
ans = keyboard.nextLine();

if(ans.equals("a"))
{
	//whatever
}

//etc.

it's super clunky and I'm sure there's a better solution, but it should work

hopefully

hopefully...

Snorlax: i7 5820k @4.5ghz, Asus X99 Pro, 32gb Corsair Vengeance LPX 2666, Cryorig R1 Ultimate, Samsung 850 evo 500gb, Asus GTX 1080 ROG Strix, Corsair RM850x, NZXT H440, Hue+

Smallsnor: Huawei Matebook X

 

Canon AE-1 w/ 50mm f/1.8 lens

Pentax KM w/ 55mm f/1.8 SMC lens

Zenit-E w/ 58mm f/2 Helios lens

Panasonic G7 with 14-42mm f/3.5 lens

Polaroid Spectra System

 

Link to post
Share on other sites

Just now, shadowbyte said:

you could use a Scanner to take user input, and specify in the question it must be "a, b, c, d"

sort of like this:


String ans;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please answer a, b, c, or d");
System.out.println("What is your favorite type of aardvark? \n a. eastern \n b. western \n c. northern \n d. midwestern");
ans = keyboard.nextLine();

if(ans.equals(a))
{
	//whatever
}

//etc.

it's super clunky and I'm sure there's a better solution, but it should work

hopefully

hopefully...

Okay! I'll give that a shot too, thank you!

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to post
Share on other sites

3 minutes ago, shadowbyte said:

you could use a Scanner to take user input, and specify in the question it must be "a, b, c, d"

sort of like this:


String ans;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please answer a, b, c, or d");
System.out.println("What is your favorite type of aardvark? \n a. eastern \n b. western \n c. northern \n d. midwestern");
ans = keyboard.nextLine();

if(ans.equals("a"))
{
	//whatever
}

//etc.

it's super clunky and I'm sure there's a better solution, but it should work

hopefully

hopefully...

Can also use a Switch statement.

 

switch(ans){
	case "a":
		//code
	break;

//etc.

default:
	//code
break;
}

 

OP, btw, I don't remember there being any Scanners on the AP CS exam or in the curriculum last year. We only used them in class mostly for fun lol.

-----> Official Unofficial Favorite Keyswitch Type Survey <-----

 OWNER OF THE FASTEST INTEL iGPU ON LTT UNIGINE SUPERPOSITION [lol]

 

GAMING RIG "SNOWBLIND"

CPU i5-13600k | COOLING Corsair H150i Elite Capellix 360mm (White) | MOTHERBOARD Gigabyte Z690 Aero G DDR4 | GPU Gigabyte RTX 3070 Vision OC (White) | RAM  16GB Corsair Vengeance Pro RGB (White)SSD Samsung 980 Pro 1TB | PSU ASUS STRIX 850W (White)CASE  Phanteks G360a (White) | HEADPHONES  Beyerdynamic DT990 Pro | KEYBOARD Zoom75 (KTT Strawberry w/ GMK British Racing Green keycaps) | MOUSE  Cooler Master MM711 (White) MONITOR HP X32 1440p 165hz IPS

 

WORK RIG "OVERPRICED BRICK"

Mac Studio (M2 Ultra / 128GB / 1TB) | HEADPHONES  AirPods Pro 2 | KEYBOARD Logitech MX Mechanical Mini | MOUSE  Logitech MX Master 3S MONITOR 2x Dell 4K 32"

 

SECONDARY RIG "ALCATRAZ"

CPU i7-4770K OC @ 4.3GHz | COOLING Cryorig M9i (review| MOTHERBOARD ASUS Z87-PROGPU Gigabyte 1650 Super Windforce OC | RAM  16GB Crucial Ballistix Sport DDR3 1600 MHzSSD Samsung 860 Evo 512GB | HDD Toshiba 3TB 7200RPMPSU EVGA SuperNOVA NEX 750WCASE  NZXT H230 | HEADPHONES  Sony WH-1000XM3  | KEYBOARD Corsair STRAFE - Cherry MX Brown | MOUSE  Logitech G602 MONITOR LG 34UM58-P 34" Ultrawide

HOLA NIGHT THEMERS

GET YOUR ASS ON NIGHT THEME

OTHER TECH I OWN:

MacBook Pro 16" [M1 Pro/32GB/1TB] | 2022 Volkswagen GTI | iPhone 14 Pro | Sony a6000 | Apple Watch Series 8 45mm | 2018 MBP 15" | Lenovo Flex 3 [i7-5500U, HD5500 (fastest on the forum), 8GB RAM, 256GB Samsung 840 Evo] | PS5, Xbox One & Nintendo Switch [Home Theater setup] | DJI Phantom 3 Standard | AirPods 2 | Jaybird Freedom (two pairs) & X2 [long story, PM if you want to know why I have 3 pairs of Jaybirds]

 

Link to post
Share on other sites

Just now, Shiv78 said:

Can also use a Switch statement.

 


switch(ans){
	case "a":
		//code
	break;

//etc.

default:
	//code
break;
}

 

OP, btw, I don't remember there being any Scanners on the AP CS exam or in the curriculum last year. We only used them in class mostly for fun lol.

Okay, gotcha

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to post
Share on other sites

I know this is a bit late, but I put together a little demo and posted it on GitHub if you want to check it out. I used a couple of different design patterns you'll probably use in later courses if you stick with it and a few different operators that are new to me as well. I split it up more than I had to, but I think its good to start thinking of how to modularize your code early on. Hopefully it helps you out a bit.

 

As for enums, there's no reason to make an instance of them, the point is that they are static so when you want to use one you can just use Schools.FIRE for example with no extra work.

 

For easy access if you don't feel like dealing with github, WARNING UN-FORMATTED CODE DUMP AHEAD TURN BACK NOW

Spoiler

public class QuizInitializer {
	
	public static final String Q1_QUESTION = "Which of these is the most powerful?";
	public static final String Q1_OPT_1 = "Volcano";
	public static final String Q1_OPT_2 = "Blizzard";
	public static final String Q1_OPT_3 = "Tornado";
	public static final String Q1_OPT_4 = "Sandstorm";
	
	public static final String Q2_QUESTION = "What is your favorite activity?";
	public static final String Q2_OPT_1 = "Competitive Sports";
	public static final String Q2_OPT_2 = "Learning something new";
	public static final String Q2_OPT_3 = "Playing with pets";
	public static final String Q2_OPT_4 = "Reading, playing games";
	public static final String Q2_OPT_5 = "Camping or fishing";
	
	public static final String Q3_QUESTION = "What is your favorite time of day?";
	public static final String Q3_OPT_1 = "Morning";
	public static final String Q3_OPT_2 = "Afternoon";
	public static final String Q3_OPT_3 = "Night";

	public static void main(String[] args) {

		//Set up test Questions
		QuizQuestion q1 = new QuizQuestion();
		q1.setQuestion(Q1_QUESTION);
		q1.addAllOptions(Q1_OPT_1,
					Q1_OPT_2,
					Q1_OPT_3,
					Q1_OPT_4);
		
		QuizQuestion q2 = new QuizQuestion();
		q2.setQuestion(Q2_QUESTION);
		q2.addAllOptions(Q2_OPT_1,
					Q2_OPT_2,
					Q2_OPT_3,
					Q2_OPT_4,
					Q2_OPT_5);
		
		
		QuizQuestion q3 = new QuizQuestion();
		q3.setQuestion(Q3_QUESTION);
		q3.addAllOptions(Q3_OPT_1,
					Q3_OPT_2,
					Q3_OPT_3);
		
		//Put the questions in a quiz
		WizardQuiz quiz = new WizardQuiz();
		quiz.addAllQuestions(q1, q2, q3);
		
		//Take the quiz
		quiz.takeQuiz();
		System.out.println("Your school is: " + quiz.getResults());		
	}

}

public abstract class Quiz {
	
	private final Scanner input = new Scanner(System.in);
	
	List<QuizQuestion> questions;
	List<String> answers;
	
	public Quiz(){
		questions = new ArrayList<>();
		answers = new ArrayList<>();
	}
	
	public void addAllQuestions(QuizQuestion... questions){
		for(QuizQuestion question: questions){
			this.questions.add(question);
		}
	}
	
	public void addQuestion(QuizQuestion question){
		this.questions.add(question);
	}
	
	public List<String> getAnswers(){
		return answers;
	}
	
	public void takeQuiz(){
		questions.forEach(q ->{
			String answer = null;
			while(answer == null){
				System.out.println(q.getQuestion());
				List<String> options = q.getOptions();
				for(int i = 0; i < options.size(); i++){
					String option = options.get(i);
					System.out.println("[" + (i+1) + "]" + option);
				}
				System.out.print("> ");
				try{
					int index = input.nextInt()-1;
					if(index >= 0 && index < options.size()){
						answer = options.get(index);
					}else{
						System.out.println("You must a valid answer!");
					}
				}catch(InputMismatchException e){
					System.out.println("You must input a number!");
					//clear the buffer so its not an infinite loop
					input.nextLine();
				}
			}
			answers.add(answer);
		});
	}
	
	public abstract String getResults();

}

public class WizardQuiz extends Quiz{
	
	double fireTotal;
	double iceTotal;
	double windTotal;
	
	public WizardQuiz(){
		resetValues();
	}

	@Override
	public String getResults() {
		resetValues();
		updateValues();
		return getMaxSchool();
	}
	
	public void resetValues(){
		fireTotal = 0;
		iceTotal = 0;
		windTotal = 0;
	}
	
	private String getMaxSchool(){
		double[] schoolArray = {fireTotal, iceTotal, windTotal};
		int maxIndex = 0;
		for(int i = 1; i < schoolArray.length; i++){
			if(schoolArray[i] > schoolArray[maxIndex]){
				maxIndex = i;
			}
		}
		
		switch(maxIndex){
			case 0: 
				return "School of Fire";
			case 1:
				return "School of Ice";
			case 2:
				return "School of Wind";
			default:
				return "School of Error";
		}
		
	}
	
	public void updateValues(){
		String a1 = answers.get(0);
		switch(a1){
			case QuizInitializer.Q1_OPT_1: //Volcano
				fireTotal += 10;
				iceTotal  += -5;
				windTotal += 1;
				break;
			case QuizInitializer.Q1_OPT_2: //Blizzard
				fireTotal += -5;
				iceTotal  += 10;
				windTotal += 5;
				break;
			case QuizInitializer.Q1_OPT_3: //Tornado
				fireTotal += 2;
				iceTotal  += 0;
				windTotal += 10;
				break;
			case QuizInitializer.Q1_OPT_4: //Sandstorm
				fireTotal += 3;
				iceTotal  += -1;
				windTotal += 7;
				break;
		}
		
		String a2 = answers.get(1);
		switch(a2){
			case QuizInitializer.Q2_OPT_1: //Competitive Sports
				fireTotal += 10;
				iceTotal  += 7;
				windTotal += 5;
				break;
			case QuizInitializer.Q2_OPT_2: //Learning something new
				fireTotal += 0;
				iceTotal  += 10;
				windTotal += 5;
				break;
			case QuizInitializer.Q2_OPT_3: //Playing with pets
				fireTotal += -4;
				iceTotal  += 3;
				windTotal += 4;
				break;
			case QuizInitializer.Q2_OPT_4: //Reading, playing games
				fireTotal += 3;
				iceTotal  += -1;
				windTotal += 7;
				break;
			case QuizInitializer.Q2_OPT_5: //Camping or fishing
				fireTotal += -5;
				iceTotal  += 7;
				windTotal += -2;
				break;
		}
		
		String a3 = answers.get(2);
		switch(a3){
			case QuizInitializer.Q3_OPT_1: //Morning
				fireTotal += 10;
				iceTotal  += 7;
				windTotal += 5;
				break;
			case QuizInitializer.Q3_OPT_2: //Afternoon
				fireTotal += 0;
				iceTotal  += 10;
				windTotal += 5;
				break;
			case QuizInitializer.Q3_OPT_3: //Night
				fireTotal += -4;
				iceTotal  += 3;
				windTotal += 4;
				break;
		}
	}

}

public class QuizQuestion {
	private String question;
	private List<String> options;
	
	public QuizQuestion(){
		options = new ArrayList<>();
	}
	
	public void addOption(String option){
		options.add(option);
	}
	
	public void addAllOptions(String... options){
		for(String option: options){
			this.options.add(option);
		}
	}
	
	public List<String> getOptions(){
		return options;
	}
	
	public void setQuestion(String question){
		this.question = question;
	}
	
	public String getQuestion(){
		return question;
	}
}

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×