Java exercises PLEASE help...should be easy for most java users!!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryann18
    New Member
    • Oct 2006
    • 22

    Java exercises PLEASE help...should be easy for most java users!!!!

    1. Write a while loop that verifies that teh user enters a positive integer value.

    2. Write a do loop that verifies that teh user enters an even integer value.

    3. Write a for loop to print the odd numbers from 1 to 99 (inclusive).

    4. Write a for loop to print the multiples of 3 from 300 down to 3.

    5. Write a method called alarm that prints the string "Alarm!" multiple times on
    seperate lines. The method should accept an integer parameter that
    specifies how many times the string is printed. Print an error message if
    the parameter is less than 1.

    6. Write a method called sumRange that accepts two integers parameters that
    represent a range. Issue an error message and return zero if the second
    parameter is less than the first. Otherwise, the method should return the
    sum of integers in that range (inclusive).
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ryann18
    1. Write a while loop that verifies that teh user enters a positive integer value.

    2. Write a do loop that verifies that teh user enters an even integer value.

    3. Write a for loop to print the odd numbers from 1 to 99 (inclusive).

    4. Write a for loop to print the multiples of 3 from 300 down to 3.

    5. Write a method called alarm that prints the string "Alarm!" multiple times on
    seperate lines. The method should accept an integer parameter that
    specifies how many times the string is printed. Print an error message if
    the parameter is less than 1.

    6. Write a method called sumRange that accepts two integers parameters that
    represent a range. Issue an error message and return zero if the second
    parameter is less than the first. Otherwise, the method should return the
    sum of integers in that range (inclusive).
    You start.

    Comment

    • ryann18
      New Member
      • Oct 2006
      • 22

      #3
      Im not even sure how to start any of them, this is what I have for #2:

      import javax.swing.JOp tionPane;
      public class KittyKitty {

      public static void main (String[] args)
      {
      String numStr, result;

      do
      {
      System.out.prin tln ("Enter an integer: ");
      result = "That number is " + ((num%2 == 0) ? "even" : "odd");
      System.out.prin tln (result);

      }
      while (result > 0);
      }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ryann18
        Im not even sure how to start any of them, this is what I have for #2:

        import javax.swing.JOp tionPane;
        public class KittyKitty {

        public static void main (String[] args)
        {
        String numStr, result;

        do
        {
        System.out.prin tln ("Enter an integer: ");
        result = "That number is " + ((num%2 == 0) ? "even" : "odd");
        System.out.prin tln (result);

        }
        while (result > 0);
        }
        }
        No that is very bad. Now let's start with number 1, but first a little info and we will be underway.
        Are you using jdk1.5 or jdk1.4?
        Are familiar with javax.swing.JOp tionPane?
        Have you done exception handling?

        That I believe is all we need to get started on number one.

        Comment

        • ryann18
          New Member
          • Oct 2006
          • 22

          #5
          I'm using JDK1.5 with netbeans, nah I'm not familiar and I've never used that.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by ryann18
            I'm using JDK1.5 with netbeans, nah I'm not familiar and I've never used that.
            Here are the swing components in action. Notice that your program lacked a way of taking in input from the user. Now be honest and say where you do not understand in this code.

            Code:
            import javax.swing.*;
            public class Number1 {
            	public static void main(String[] args) {
            		String input = JOptionPane.showInputDialog(null, "Enter a number");
            		int i = input.length() - 1;
            		boolean valid = true;
            		while(i > 0) {
            			if(!Character.isDigit(input.charAt(i))) {
            				valid = false;
            				break;
            			}
            			i--;
            		}
            		if(!valid) {
            			JOptionPane.showMessageDialog(null, input+" is not a positive integer");
            		}
            		else {
            			JOptionPane.showMessageDialog(null, input+" is a positive integer");
            		}
            	}
            }

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Of course moving back one decade, we have the non-swing solution

              Code:
              import java.util.Scanner;
              public class Number1 {
              	public static void main(String[] args) {
              		Scanner scan = new Scanner(System.in);
              		System.out.print("Enter your input and press Enter: ");
              		String input = scan.nextLine();
              		int i = input.length() - 1;
              		boolean valid = true;
              		while(i > 0) {
              			if(!Character.isDigit(input.charAt(i))) {
              				valid = false;
              				break;
              			}
              			i--;
              		}
              		if(!valid) {
              			System.out.println(input+" is not a positive integer");
              		}
              		else {
              			System.out.println(input+" is a positive integer");
              		}
              	}
              }

              Comment

              • ryann18
                New Member
                • Oct 2006
                • 22

                #8
                Originally posted by r035198x
                Of course moving back one decade, we have the non-swing solution

                Code:
                import java.util.Scanner;
                public class Number1 {
                	public static void main(String[] args) {
                		Scanner scan = new Scanner(System.in);
                		System.out.print("Enter your input and press Enter: ");
                		String input = scan.nextLine();
                		int i = input.length() - 1;
                		boolean valid = true;
                		while(i > 0) {
                			if(!Character.isDigit(input.charAt(i))) {
                				valid = false;
                				break;
                			}
                			i--;
                		}
                		if(!valid) {
                			System.out.println(input+" is not a positive integer");
                		}
                		else {
                			System.out.println(input+" is a positive integer");
                		}
                	}
                }
                Where does the while loop go in this?

                For the second one would I just do basically the same thing except add num%2 for it to become even??

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  What do you mean by
                  Originally posted by ryann18
                  Where does the while loop go in this?
                  ?

                  Comment

                  • ryann18
                    New Member
                    • Oct 2006
                    • 22

                    #10
                    Originally posted by r035198x
                    What do you mean by ?
                    Sorry I meant is there any way I can only do a while without an if / else statement?

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by ryann18
                      Sorry I meant is there any way I can only do a while without an if / else statement?
                      Not for this one. You have to test that the input satisfies the given conditions and the if/else is tailor made for that.
                      Now in that Number1, the while condition should read while(i >= 0) for reasons that should now be obvious. I have to knock off to bed now (9:54pm here).

                      Number 2 does not say the number should be positive. So my solution includes - sign at the start of the number. Here is how you would do it. Now hopefully by tommorrow you will have walked through the rest of the exercises.

                      Code:
                      import java.util.Scanner;
                      public class Number1 {
                      	public static void main(String[] args) {
                      		Scanner scan = new Scanner(System.in);
                      		System.out.print("Enter your input and press Enter: ");
                      		String input = scan.nextLine();
                      		int i = input.length() - 1;
                      		boolean valid = true;
                      		do {
                      			if(i == 0) {
                      				if(!(Character.isDigit(input.charAt(i)) || input.charAt(i) == '-')) {
                      					valid = false;
                      					break;
                      				}
                      			}
                      			else if(!Character.isDigit(input.charAt(i))) {
                      				valid = false;
                      				break;
                      			}
                      			i--;
                      		}
                      		while(i >= 0);
                      		if(!valid) {
                      			System.out.println(input+" is not a Number");
                      		}
                      		else {
                      			int integer = Integer.parseInt(input);
                      			if(integer % 2 ==0) {
                      				System.out.println(input+" is a positive even number");
                      			}
                      			else {
                      				System.out.println(input+" is not even");
                      			}
                      		}
                      	}
                      
                      
                      }

                      Comment

                      • ryann18
                        New Member
                        • Oct 2006
                        • 22

                        #12
                        Thanks a lot!!!

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by ryann18
                          Thanks a lot!!!
                          Hope you finish up the rest on the exercises.

                          Comment

                          Working...