Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess

I need help. Please someone help me. Write a java program called PrintNumberInWord which printss "ONE","TWO",....,"NINE","OTHER" if the int variable "number" is 1,2,....,9, or other respectively. Use (a)a "nested if" statement (b)a "switch case" statement.

  • one year ago
  • one year ago

  • This Question is Closed
  1. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    Following are my codings for the above question. (a) public class PrintNumberInWord { public static void main(String[]args) { int number=8; if(number==1) { System.out.print("ONE"); } else if(number==2) { System.out.print("TWO"); } else if(number==3) { System.out.print("THREE"); } else if(number==4) { System.out.print("FOUR"); } else if(number==5) { System.out.print("FIVE"); } else if(number==6) { System.out.print("SIX"); } else if(number==7) { System.out.print("SEVEN"); } else if(number==8) { System.out.print("EIGHT"); } else if(number==9) { System.out.print("NINE"); } else { System.out.print("OTHER"); } } }

    • one year ago
  2. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    (b)public class PrintNumberInWord1 { public static void main(String[]args) { int number=5; switch(number) { case 1: System.out.print("ONE"); break; case 2: System.out.print("TWO"); break; case 3: System.out.print("THREE"); break; case 4: System.out.print("FOUR"); break; case 5: System.out.print("FIVE"); break; case 6: System.out.print("SIX"); break; case 7: System.out.print("SEVEN"); break; case 8: System.out.print("EIGHT"); break; case 9: System.out.print("NINE"); break; default: System.out.print("OTHER"); } } }

    • one year ago
  3. Chris2332
    Best Response
    You've already chosen the best response.
    Medals 1

    Right so... before Java 5: public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; public static void main(String[] args) { int size = toppings.length; for (int i=0; i<size; i++) { System.out.println(toppings[i]); } } } After Java 5: public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; public static void main(String[] args) { int size = toppings.length; for (String s: toppings) { System.out.println(s); } } } Does that help?

    • one year ago
  4. Chris2332
    Best Response
    You've already chosen the best response.
    Medals 1

    Oops... sorry... didn't even read what you need. But what is required to use seems a bit clunky and not good practice I would imagine. I would be easier to use the following if you are given a number: int number=5; private String[] numbers= {"ONE","TWO",....,"NINE"}; if(number>9) { System.out.println("OTHER"); } else { System.out.println(numbers[number-1]); }

    • one year ago
  5. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks a lot for the help @chris2332. But they want us to use only "nested if" not arrays. Is my answer right for the question? I am not sure.

    • one year ago
  6. Chris2332
    Best Response
    You've already chosen the best response.
    Medals 1

    Err... yes... the code is straight forward for if statements or switch/case statements!

    • one year ago
  7. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    Isn't nested if this if(a>0) { if(b<0) { } } Hw do we use this for the above queastion? That's vat is confusing me:(

    • one year ago
  8. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    @Chris2332

    • one year ago
  9. JakeV8
    Best Response
    You've already chosen the best response.
    Medals 0

    it wouldn't seem necessary, but you could force a nesting by first checking for the "other" case where the int is not 0-9, and then if it is 0-9, run through your print ONE, etc. cases.

    • one year ago
  10. JakeV8
    Best Response
    You've already chosen the best response.
    Medals 0

    That would be nested, but it seems weird to nest something like that... like you said, it makes more sense when you have two separate conditionals, not just different outcomes of the same conditional.

    • one year ago
  11. JakeV8
    Best Response
    You've already chosen the best response.
    Medals 0

    (disclaimer, I have about 6 total hours of Java time... I came here looking for Python posts and saw your thread :) )

    • one year ago
  12. ajprincess
    Best Response
    You've already chosen the best response.
    Medals 0

    :) Bigg thanks for your help:)

    • one year ago
  13. JakeV8
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't know how helpful that was :) but thanks :)

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.