Declared boolean on object, doesn't change his value [closed] - java

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 9 years ago.
Improve this question
I was just figuring out how instance variables are working, but it lead me to something strange.
public class DrumKit {
boolean topHat = true;
boolean snare = true;
void playTopHat() {
System.out.println("ding ding da-ding");
}
void playSnare(){
System.out.println("bang bang ba-bang");
}
}
public class DrumKitTestDrive {
public static void main(String [] args) {
DrumKit d = new DrumKit();
d.playSnare();
d.snare = false;
d.playTopHat();
if (d.snare == true);{
d.playSnare();
}
}
}
How is it possible that it outputs:
"bang bang ba-bang
ding ding da-ding
bang bang ba-bang"
And not: "bang bang ba-bang
ding ding da-ding"
Because what I thought was that the snare only would play once because I'm declaring it after d.playSnare();
to d.snare = false;

Remove the semicolon in this line, which is acting as the body for your if block.
if (d.snare == true);{
Change it to
if (d.snare == true){
Also, d.snare is already a boolean, so you can simplify the conditional expression to:
if (d.snare){

Here's the bang
if (d.snare == true);
Remove ;

Related

this code of mine has no error but it isnt showing any output [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 3 years ago.
Improve this question
In this code the method setEid isn't working dont know what's the problem with while loop
this code of mine has no error but it isnt showing any output
public class Emp {
private String eid;
public String getEid() {
return eid;
}
public void setEid(String e) {
while (e.length() < 12) {
eid = e;
}
}
}
public class Test {
public static void main(String[] args) {
Emp z = new Emp();
z.setEid("rgrge");
System.out.println("\n" + z.getEid());
}
}
enter code here i expect the static initialization of setEid argument should not take more than 12 characters
Your setter method have a while loop if eid length is less than 12 then it always stuck in this method.
public void setEid(String e){
while(e.length()<12)
eid=e;
}
and In your main method
public static void main(String[] args) {
Emp z=new Emp();
z.setEid("rgrge"); // you call setter
System.out.println("\n"+z.getEid());
}
you pass "rgrge" in setter and its length is less than 12. Tht's why your program is stuck in loop and not showing any thing.
Change setter implementation to this.
public void setEid(String e){
if(e.length()<12) // change while to if
eid=e;
}
so first there should have had if instead of while.
Change:
while(e.length()<12) change this as if(e.length()<12)
Explanation:
your setEid method never going to run because there is no break condition there. your while loop condition tends to the infinite. your code gets stuck in that while loop.
here you passed z.setEid("rgrge");. in your example, Length of string e is 5 which is obviously less than 12, so the condition will always be satisfied and your program will be stuck in infinite loop.

Detect the programming language [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 6 years ago.
Improve this question
Can anyone please tell me what is this coding language? It has similar structure to Java but I cannot understand.
class Sample {
String student;
#Override
public String toString() {
return "${student}";
}
public static void main(String[] args) {
Sample s = new Sample();
s.student = "Joe"
System.out.println(s.toString())
}
}
This piece of code is valid in groovy... and print "Joe" on standard output.
It is Java, no discussion on this.
But 2 errors :
class Sample {
String student;
#Override
public String toString() {
return student; //<------------------ and not "${student}"
}
public static void main(String[] args) {
s.student = "Joe"; //<---------------- don't forget ';'
}
}
First error is intuitive, I mean you can return "${student}; it will print it, but it's not what the user wanted to do, quite sure

Why call to " this " must be first statement in constructor? [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 6 years ago.
Improve this question
I am trying to execute this below code sample to understand why call to " this " must be first statement in constructor ?? I had read lot of About it and I understand Why this is so !!
so I write the below simple program But Still showing me the same Error even I use 'this' as a First Statement in my Program .
import java.io.*;
import java.lang.*;
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
class ThisDemo
{
public static void main(String []args)
{
Demo obj = new Demo();
}
}
To specifically answer your question, this or super needs to be the first call to ensure the base class has been setup correctly.
https://stackoverflow.com/a/1168356/154186
To solve your error above, remove the void type from the function call. e.g.:
Demo(int x) {
this.x = x;
}
Demo() {
this(50);
}
Remove void from Demo constructors
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
You should remove void.Constructor must have no explicit return type.
Then it will work fine.

Java method errors [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 7 years ago.
Improve this question
I'm new to Java and was trying to compile a program but there's a problem with on of my methods. Here is the code for the one that's giving me trouble.
public String nameCard (); {
String namedSuit;
String namedCard;
String namedFace;
if (this.suit==0) {
namedSuit="Spades";
}
if (this.suit==1) {
namedSuit="Hearts";
}
if (this.suit==2) {
namedSuit="Clubs";
}
if (this.suit==3) {
namedSuit="Diamonds";
}
if (this.face==1 || this.face>10) {
if (this.face==1) {
namedFace="Ace";
}
if (this.face==11) {
namedFace="Jack";
}
if (this.face==12) {
namedFace="Queen";
}
if (this.face==13) {
namedFace="King";
}
namedCard=namedFace + " of " + namedSuit;
}
else if (this.face>1 && this.face<=10) {
namedCard=this.face + " of " + namedSuit;
}
return namedCard;
}
The cmd window keeps returning "Card.java:70(the first line I have here): error: missing method, body, or declare abstract" and "Card.java:104: error: return outside method" for the last line with text. Any help would be greatly appreciated!
A semicolon is an empty statement. Remove it in your method declaration. This
public String nameCard (); {
Should be
public String nameCard () {

Why is it showing ";" expected in main statement while compiling? [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 8 years ago.
Improve this question
Not able to compile and it is showing ; expected at the end of public static statement.
//Recursive program
class Recur
{
public static void main(String args[])
void witty(String n, int p)
{
if(p<0)
System.out.println("");
else
{
System.out.println(n.charAt(p)+".");
witty(n, p-1);
System.out.print(n.charAt(p));
}
}
}
It seems that you forgot the body of the main method:
public static void main(String args[]) {
}
Note that the body of a method is delimited by { and }.

Categories