r/learnjava • • Jan 27 '20

Here are a few beginner exercises for you

Round 2

Here are a few beginner java/programming exercises for well, uhh, beginners to get out of that Tutorial watching phase. Try to complete these without one but you are allowed to search the web on how to implement stuff like a Scanner or stuff like that. Here is my GitHub repo for solutions(Note: Try to do it without my Github because the end goal is to teach you how to solve stuff. Your solution can differ, the end goal is the solve the problem)

Ill make more posts like these and update my Github with more programs from beginner to intermediate and some algorithm and design patterns. So keep on the lookout for stuff like this!

The exercises are:

  1. A dice roll program. You press start and it "rolls" the "dice" and prints a random number from 1-6

  2. A fortune cookie program: You press start and it prints a random fortune cookie message.

  3. A console based calculator. You press start and it takes 3 inputs : The first number, the second number and the operation. Then it prints the result.

Good luck!

59 Upvotes

17 comments sorted by

4

u/Mighel88 Jan 27 '20

Can we have more so questions to learn ?(I am absolutely beginner)

1

u/ThatRandomGamerYT Jan 27 '20

I am a beginner too. Kinda. I am 15 learning java on/off since 6th grade(2016, I am gonna be in 10th grade in 2 months) I have finally done something other than a tutorial and light modding. I wanted to share this stuff with others. Ill post more in a few days. My 9th grade finals start from 3rd Feb till 17th. So I dont have much time right now.

But.... I would suggest adding more stuff to the console calculator like squares, cubes, roots. Or Make a calculator that calculates area, perimeter, volume etc.

3

u/Java_Junior Jan 27 '20

oh man I wish I started programming at 15. You will have a very bright future

1

u/ThatRandomGamerYT Jan 27 '20

Consider "watching" github repo for more stuff!

2

u/ctoner2653 Jan 29 '20

Really impressive to have such an interest in java at a younger age, good stuff :)

4

u/desrtfx Jan 27 '20 edited Jan 27 '20

DiceRoll.java:

int dice = diceNum.nextInt(7); // Using 6 as the index would set the range as 0-5. 7 sets it as 1-6. Weird Java quirck

That is plain wrong. The range of .nextInt(int range) always starts at 0. So, when you want the range 1 to 6, you need .nextInt(6)+1


Calcu.java:

switch(opperand)  {
	case "+":
		sum = firstNum + secondNum;
		System.out.println( "Sum is" + " " + sum); //I kind of forgot how to print a variable with a "" but later on i remembered, lol
		break;
	case "-":	
		sum = firstNum - secondNum;
		System.out.println( "Sum is" + " " + sum);
		break;
	case "*":	
		sum = firstNum * secondNum;
		System.out.println( "Sum is" + " " + sum);
		break;
	case "/":	
		sum = firstNum / secondNum;
		System.out.println( "Sum is" + " " + sum);
		break;

Only Addition has a sum. Subtraction has a difference, multiplication has a product, division has a quotient.

Also, check for spelling mistakes. It's operand, not opperand.

A learning resource must also be semantically correct and with proper spelling.

System.out.println( "Sum is" + " " + sum);

Simpler:

System.out.println( "Sum is " + sum);

And best:

System.out.printf( "Sum is %d%n", sum);

Also, a learning resource should follow clean code principles.

3

u/ThatRandomGamerYT Jan 27 '20

Thanks for the constructive criticism! I'll fix these mistakes. I am no pro, just a 15 y/o kid

4

u/[deleted] Jan 27 '20

No need to tell people how old you are online. Just keep doing your thing. 15 or 45. Doesn't matter.

2

u/MyNameIsRichardCS54 Jan 27 '20

(3) Building a full RPN calculator is a great introduction to the stack data structure. It's probably a level or two up from these though.

1

u/ThatRandomGamerYT Jan 27 '20

Ill keep that in mind!

1

u/grumtaku Jan 27 '20

In our cs lessons they made us implement a program which gets 3 variables and a calculation as a input then calculates it.

Example input:

int a = 4;

int b = 3 ;

double c = 2.1;

a*2+ b*c ;

result = 14.3

In inputs number of spaces and tabs can change and different doubles and integers may be in calculations( a *2 + c );

A variable can be used more than once or may never be used.

Variable names can vary.

Parenthesis and nested ones may be present [ ( a + (b+ 2)*c)*2 ]

And you need to make calculations in correct order as the compiler does.

In addition you need to return your result as a double or integer ( 14 and 14.0 are different answers)

This project was lots of fun to do and we showed remarkable progress at the end, so I wanted to add.

1

u/[deleted] Jan 27 '20

3 Is known as reverse Polish notation.

1

u/[deleted] Feb 15 '20

STOP posting your convoluted solutions here. Your code is simply unreadable and hardly ever logical. Stop misleading beginners with your incorrect code.