r/learnjava • u/ThatRandomGamerYT • Jan 27 '20
Here are a few beginner exercises for you
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:
A dice roll program. You press start and it "rolls" the "dice" and prints a random number from 1-6
A fortune cookie program: You press start and it prints a random fortune cookie message.
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!
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 quirckThat 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
Jan 27 '20
No need to tell people how old you are online. Just keep doing your thing. 15 or 45. Doesn't matter.
1
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
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
1
1
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.
4
u/Mighel88 Jan 27 '20
Can we have more so questions to learn ?(I am absolutely beginner)