Hi everyone , i hope someone helps me out.
im working my way up step by step learning Java . but no one at the University is helping , they just give us the homework and we have to learn alone.and give the soltution at the end of the week. the problem is i cant make mistakes cuz if do i will not be allowed to enter the final exam at all. so its like challenging the students not teaching them...

i would be very thankful to the one who helps me out , and be sure that i will learn it carefully b4 i upload it to the classTutor. ( im studying Informationtechnology not computer science ) i only have Java as programming class in my study plz help me out .

this week we have these Questions.

1- write a java programm which calculate the Faculity of a given numbe n (n!).
2-a) write a java programm which reads a typed number and prints it out : if n neative : "input number is negative"
if n positiv : "input number is positiv" , if n = 0 : "input number is Zero"
2-b) epand your program : if input number n is positiv then : is it odd or even ? ( use if else struct inside the posivie 2-a) )
2-c) expand your program : if the input nuber is between 1 - 7 then print out the corresponding day 1 = Monday .... 7 = sunday ( use Switch construct )
3-a) write a java program that : reads 10 numbers and gives out the max . min , and the Midvalue ( use "for loop")
3-b) implement the program from 3-a) with "while loop"
3-c) implement the program from 3-a) with "do-while-loop" in this case positiv numbers are to be typed as input and as soon as the input number is negative the program will give the results (min , max , midvalue ) and stop .
4- make a java-class which solve Question 1 (n!) with for-loop . the number n is to be typed with the keyboard.

thanks allot

Recommended Answers

All 9 Replies

There are lots of people here who will freely give their time to help you become the best Java programmer you can be.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

1- write a java programm which calculate the Faculity of a given numbe n (n!).
2-a) write a java programm which reads a typed number and prints it out : if n neative : "input number is negative"
if n positiv : "input number is positiv" , if n = 0 : "input number is Zero"
2-b) epand your program : if input number n is positiv then : is it odd or even ? ( use if else struct inside the posivie 2-a) )
2-c) expand your program : if the input nuber is between 1 - 7 then print out the corresponding day 1 = Monday .... 7 = sunday ( use Switch construct )
3-a) write a java program that : reads 10 numbers and gives out the max . min , and the Midvalue ( use "for loop")
3-b) implement the program from 3-a) with "while loop"
3-c) implement the program from 3-a) with "do-while-loop" in this case positiv numbers are to be typed as input and as soon as the input number is negative the program will give the results (min , max , midvalue ) and stop .
4- make a java-class which solve Question 1 (n!) with for-loop . the number n is to be typed with the keyboard.

Here we go:

1: No idea what is a Faculity
2a:

Scanner input=new Scanner(System.in);
        try
        {
            do
            {
                System.out.println("Insert a number");
                String num=input.next();
                if (Double.parseDouble(num)>0)
                {
                    System.out.println("Number is positive");
                }
                else if (Double.parseDouble(num)<0)
                {
                    System.out.println("Number is negative");
                }
                else if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is zero");
                }
                else
                {
                    System.out.println("You have not inserted a number. Exiting...");
                    System.exit(0);
                }

            }while(input!=null);
        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a number. Exiting...");
            System.exit(0);
        }

2b:

Scanner input=new Scanner(System.in);
        try
        {
            do
            {
                System.out.println("Insert a number");
                String num=input.next();
                if (Double.parseDouble(num)>0)
                {
                    System.out.println("Number is positive");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else if (Double.parseDouble(num)<0)
                {
                    System.out.println("Number is negative");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is cero");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else
                {
                    System.out.println("You have not inserted a number. Exiting...");
                    System.exit(0);
                }

            }while(input!=null);
        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a number. Exiting...");
            System.exit(0);
        }

2c

Scanner input=new Scanner(System.in);
        try
        {
            do
            {
                System.out.println("Insert a number");
                String num=input.next();
                if (Double.parseDouble(num)>0)
                {
                    System.out.println("Number is positive");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                    if (Double.parseDouble(num)>=1 && Double.parseDouble(num)<=7)
                    {
                        switch(num)
                        {
                            case "1":
                            {
                                System.out.println("Day is Sunday");
                                break;
                            }
                            case "2":
                            {
                                System.out.println("Day is Monday");
                                break;
                            }
                            case "3":
                            {
                                System.out.println("Day is Tuesday");
                                break;
                            }
                            case "4":
                            {
                                System.out.println("Day is Wed");
                                break;
                            }
                            case "5":
                            {
                                System.out.println("Day is Thur");
                                break;
                            }
                            case "6":
                            {
                                System.out.println("Day is Friday");
                                break;
                            }
                            case "7":
                            {
                                System.out.println("Day is Saturday");
                                break;
                            }               

                        }
                    }

                }
                else if (Double.parseDouble(num)<0)
                {
                    System.out.println("Number is negative");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is cero");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else
                {
                    System.out.println("You have not inserted a number. Exiting...");
                    System.exit(0);
                }

            }while(input!=null);
        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a number. Exiting...");
            System.exit(0);
        }

3a:

Scanner input=new Scanner(System.in);
        Double sum=0.0;
        String min="0";
        String max="0";
        try
        {
            for (int i=0;i<10;i++)
            {
                System.out.println("Insert number " + String.valueOf(i+1));
                String inserted=input.next();
                sum=sum+Double.parseDouble(inserted);
                if (Double.parseDouble(inserted)>Double.parseDouble(max))
                {
                    max=inserted;
                }
                if (Double.parseDouble(inserted)<Double.parseDouble(min))
                {
                    min=inserted;
                }




            }
            System.out.println("Max number: " + max);
            System.out.println("Min number: " + min);
            System.out.println("Ave: " + sum/10);
            System.exit(0);

        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a valid number. Exiting...");
            System.exit(0);
        }

3b:

Scanner input=new Scanner(System.in);
        Double sum=0.0;
        String min="0";
        String max="0";
        try
        {
            int i=0;
            while (i<10)
            {
                System.out.println("Insert number " + String.valueOf(i+1));
                String inserted=input.next();
                sum=sum+Double.parseDouble(inserted);
                if (Double.parseDouble(inserted)>Double.parseDouble(max))
                {
                    max=inserted;
                }
                if (Double.parseDouble(inserted)<Double.parseDouble(min))
                {
                    min=inserted;
                }
                i++;
            }
            System.out.println("Max number: " + max);
            System.out.println("Min number: " + min);
            System.out.println("Ave: " + sum/10);
            System.exit(0);

        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a valid number. Exiting...");
            System.exit(0);
        }

3c:

Scanner input=new Scanner(System.in);
        Double sum=0.0;
        String min="0";
        String max="0";
        try
        {
            int i=0;
            String inserted;
            do
            {
                System.out.println("Insert number " + String.valueOf(i+1));
                inserted=input.next();
                sum=sum+Double.parseDouble(inserted);
                if (Double.parseDouble(inserted)>Double.parseDouble(max))
                {
                    max=inserted;
                }
                if (Double.parseDouble(inserted)<Double.parseDouble(min))
                {
                    min=inserted;
                }
                i++;
            }while ((Double.parseDouble(inserted)>=0) && (i<9));
            System.out.println("Max number: " + max);
            System.out.println("Min number: " + min);
            System.out.println("Ave: " + sum/10);
            System.exit(0);

        }
        catch (NumberFormatException ex)
        {
            System.out.println("You have not inserted a valid number. Exiting...");
            System.exit(0);
        }

4: I did not understand 1 so....

commented: first time I do this -3

As for #1, it looks like it's supposed to be "factorial"; that's what n! usually means. #4 makes sense that way, too.

As for #1, it looks like it's supposed to be "factorial"; that's what n! usually means. #4 makes sense that way, too.

OK.

you may want to re-think some of the logic in there.

if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is cero");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }

see the logical error there?

in the first code you've provided, you've forgotten the possibility of a negative number.

String num=input.next();
                if (Double.parseDouble(num)>0)
                {
                    System.out.println("Number is positive");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else if (Double.parseDouble(num)<0)
                {
                    System.out.println("Number is negative");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }
                else if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is cero");
                    if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }
                }

here, you're repeating code. it would be easier to do something like this:

String num=input.next();
                if (Double.parseDouble(num)>0)
                {
                    System.out.println("Number is positive");
                }
                else if (Double.parseDouble(num)<0)
                {
                    System.out.println("Number is negative");
                }
                else if (Double.parseDouble(num)==0)
                {
                    System.out.println("Number is cero");                    
                }
                if (Double.parseDouble(num)%2==0)
                    {
                        System.out.println("Number is even");
                    }
                    else
                    {
                        System.out.println("Number is odd");
                    }

but it only had to be checked on positive numbers, so might as wel do something like this:

  int nr = myScanner.nextInt();
  if ( nr > 0 ){
    System.out.println("The number is positive");
    if ( (nr%2) == 0 )
      System.out.println("The number is even");
    else
      System.out.println("The number is odd");
  } else if ( nr == 0 ){
    System.out.println("The numer is zero");
  } else{
    System.out.println("The number is negative");
  }

you're repeating code.

In all sincerity, I have never really cared. I perfer repeating code 100 times and program takes up 100MB in memory rather than not repreat code and the program take up 1MB. To me, a program should be made so I can understand and it does what it is suppose to do.

Ive said several times I am not a great programmer, I just program (and dont like it) so....thats my point of view and Im sticking to it.

Liked that so much, its in my sig now :P

Hey you are getting philosophical!

might be you don't care about it, but the OP (or others who land in this thread looking for answers) might care about programming decent, and if they don't, chances are their teachers or employers will.

so when providing an answer, it's always a good thing to keep it as good as possible.

commented: Well said +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.