How can I find capacity of stadium using Java? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is the question
Write a method atCapacity(int people, int capacity) that returns a boolean determining if a stadium is at maximum capacity. A stadium is at capacity if at least 80% of the seats are sold out.
Here's the code I tried
public class Scratchpad
{
atCapacity(int people, int capacity)
{
boolean atCapacity = false;
capacity = people * (80/100);
if(people > capacity)
{
return false;
}
else if(people <= capacity)
{
return true;
}
}
}
The code checker says
"Grader.java: Line 4: invalid method declaration; return type
required"
I don't understand how to fix the code. I'm not asking for answers I just need a nudge in the right direction. Thanks

This is supposed to be a method declaration:
atCapacity(int people, int capacity)
But the syntax for a method declaration requires a return type, as in
SomeReturnType atCapacity(int people, int capacity)
If we look at the body of the method you are returning true and false so the correct type should be the type of true and false, which is <nudge>
I can see some other errors too (<nudges>):
The compiler will tell you that you need a return at the end of the method. (It is not going to deduce that people > capacity and people <= capacity are mutually exclusive.)
There is more than one way of solving that conundrum.
At runtime, 80/100 is going to cause you grief; see Int division: Why is the result of 1/3 == 0?.
On the top of that, you have a variable called atCapacity which doesn't appear to be needed.

you could write
public boolean atCapacity() { ... }

Related

Beginner question, incompatible types: boolean to int. Not seeing the problem [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Trying to find out if any of the digits in a number are odd, if so return true. If any are even then return false. Getting error: incompatible types: boolean cannot be converted to int. Any help is appreciated.
public class allDigitsOddTest{
public static void main(String[] args) {
allDigitsOdd(756410);
}
public static int allDigitsOdd(int num){
boolean value = true;
int evens = 0;
int odds = 0;
while (num > 0){
int remainder = num % 10;
if (remainder % 2 == 0){
evens++;
}
else{
odds++;
}
num = num / 10;
}
if (evens > 0){
value = false;
}
return value;
}
}
Your return type is int, instead of boolean
change
public static int allDigitsOdd(int num)
to
public static boolean allDigitsOdd(int num) {
In the beginning it helped me a lot to look at Methods like this:
You try to unlock your door at home, which you have the keys for. It doesn't matter which of your three keys you use, because it works with every one of your keys. But if you try your car key it won't fit and the mission is a failure.
So look at the different data types as keys, you can lock the door with your main key, and your girlfriend unlocks it later with hers.
-> One key in, another one of the same out if you understand what I mean.
Same for Java, you give an integer into the method, you can return every integer you want, but not a different data type (or key type).
Maybe an odd example, but for some reason it helped me a lot.

Print Binary using recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This code work properly. I need help to change this into int function. Please don't add extra int or void function to it.
public void binary(int n) {
if(n==0)
return;
binary(n>>1);
System.out.printf("%d",n%2);
}
Here is the changed code:
public int binary(int n)
{
if (n == 0 || n == 1) return n;
return binary(n >> 1) * 10 + n % 2;
}
It returns an int and it will return binary form of the number. This code will fail to provide correct output after the number 1023. The binary of 1023 is 1111111111.
After this, any number you enter will cause overflow issues and you will receive a wrong output. You can further increase the range of the program if you use long. However, that too will fail to provide any correct output after 524287 for the same reasons mentioned before.
You can use BigInteger class if you want to store binary of any numbers bigger than 524287.

Projekt Hangman game [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am a beginner in Java and I am working right now with a small projekt, a hangman game. One of the functions I am working on right now is a method where it takes a char input, check if the input is already added to the list or not, if it is, a message will show up saying "You have already used that character!" and the user will have to guess again, otherwise the input will be added to the list. My issue right now is that nothing is happening, inputs are not added to the list at all.
This is what I have done so far:
Any advice/help would be appreciated!
public static ArrayList<Character> getGuesses(ArrayList<Character> allGuesses, char input){
for (int i = 0; i < allGuesses.size(); i++) {
if (allGuesses.get(i) == input) {
System.out.println("You have already used that character!");
}else {
allGuesses.add(input);
}
}
return allGuesses;
}
You are adding the input character on each iteration as you search the collection. You should only add it after you have searched the collection and not found it.
for (int index = 0 ; index < allGuesses.size() ; ++index) {
if (allGuesses.get(index) == input) {
System.out.println("You already used that character!");
return allGuesses;
}
}
allGuesses.add(input);
return allGuesses;
However, this can be simplified by using the Collection contains method such that you do not employ a loop.
if (allGuesses.contains(input)) {
System.out.println("You already used that character!");
return allGuesses;
}
allGuesses.add(input);
return allGuesses;
If possible, consider switching the type of allGuesses to a Set implementation (e.g. HashSet). A Set seems to better match how you are using your collection and allows you to reduce this method to...
if (! allGuesses.add(input)) {
System.out.println("You already used that character!");
}
return allGuesses;

How can i get my PrimeNumber generator to work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers

Elegant way to return a value inside an if [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Imagine you want to write a simple method, which take a String argument named "foo". The method should return the int "1" if foo equals "toto" and "2" in the others cases. What is the best way to write this method without using a ternary expression ?
I have several solutions but can't decide by myself which one is the best in Java.
Solution 1 :
public int method1(String foo){
if("toto".equals(foo)){
return 1;
}
return 2;
}
Solution 2 :
public int method2(String foo){
if("toto".equals(foo)){
return 1;
} else {
return 2;
}
}
Solution 3 :
public int method3(String foo){
int result = 2;
if("toto".equals(foo)){
result = 1;
}
return result;
}
Solution 4 :
public int method4(String foo){
int result = 0;
if("toto".equals(foo)){
result = 1;
} else {
result = 2;
}
return result;
}
Any other ideas?
Remember, I search a solution without ternary condition.
Thanks,
EDIT : I know that all the previous methods give the expected result, so all of them can be used. What I wanted to know and what wasn't clear in my initial question is : Is there some kind of standard in the Java community about this situation ?
For example, I know there is a standard about the position of the brackets : you can put them a line after the if-statement but most of the people won't do it in Java.
I know that the ternary expression is probably the best here. But, as you can imagine, my "real" method is more complicated than that and can't used ternary. The problem I presented here is just a simplification, not the entire, real problem.
I think, the best is the most readable
public int method(String foo){
return "toto".equals(foo)?1:2;
}

Categories

Resources