Write A Program Using Looping Computer Science Essay

Published:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Nowadays, there are many program also got involved some java. What is java? Actually, java is a one kind of programming language. Just like programming c and programming C++. Both of it's also are programming language. Java programming is very useful too. Programming C and Programming C++ also use same Microsoft visual C++ program to write program. Java also got its own program and it name as JCreator. For nowadays, the latest version of JCreator is JDK version 1.6.0_20. So, java programming is very important for now and because of it can apply to many places and can let them program run more smooth.

Question one is about to require programmer to write a program to require user to choose a choice than display out the output. If the user chooses out of related choice, then the program will display out a output to tell that user has enter a wrong choice. The question have been given while loop and for loop and it means that while loop and for loop also need use syntax to display out the symbol of star but not just print out the symbol of star.

Inside the program also need use looping to loop the same question to ask user until user type a word then program will terminate. The program need require user to enter a value, so the program must have input function. If not, the program wouldn't contain any input field. If user type out of related value, then the program will require user to do what. Finally, the user has type the value so that the program will terminate.

Question two is to require programmer to write a program with switch statement to require user type the value to display out the following month. Besides that, the program also need require user to enter value so the program must have input syntax. Then inside the switch statement also contain its own syntax. If the user has type out of related value, then the program will display out a output to show that user has type wrong input. When the user has type the value and the program will display out the output and after that the program will be terminate.

Answer question one

import java.util.Scanner;

public class AssignmentQuestion1 {

public static void main(String[] args)

{

System.out.println ("1) while loop");

System.out.println ("2) for loop");

System.out.println ("");

System.out.print ("Enter your choice : ");

Scanner scanner =new Scanner(System.in);

int x = scanner.nextInt();

if (x==1)

{

System.out.println ("Using while loop:");

int a=1;

while(a<6)

{

int b=1;

while(b<=a)

{

System.out.print ("* ");

b++;

}

System.out.println ("");

a++;

}

}

else if (x==2)

{

System.out.println ("Using for loop :");

int m = 1;

for (m=1; m<6; m++)

{

int l = 1;

for (l=1; l<=m; l++)

{

System.out.print ("* ");

}

System.out.println ("");

}

}

else

{

System.out.println ("You are key in a wrong input ");

}

System.out.println ("");

int p;

for(p=1; p>0; p++)

{

System.out.print ("Do you wish to continue?[Y/N] : ");

char choice = scanner.next().charAt(0);

if(choice=='Y' || choice=='y')

{

main(args);

}

else if (choice=='N' || choice=='n')

{

System.exit(0);

}

else

{

System.out.println ("you are key in a wrong input,please try key in again.");

}

{

System.out.println ("");

}

}

}

}

Above the program is requiring user to choose choice one or choice two. After choose the choice then the program will ask the user wish to continue or not. If continue, then the program will loop back. If not, the program will terminate.

First, the code of the program is contains import java.util.Scanner. This code is to let the program contain input function for user to enter input. If the program is require user to key in any input and the program is require the code of import java.util.Scanner. Public class AssignmentQuestion2 is to start a class for the AssignmentQuestion2. Next is about the public static void main (String[] args). This part of code is to start the execution of Java Program. After that, it will display out as choice 1 and 2 to call user to choose.

Then is about Scanner scanner=new Scanner (System.in);. Scanner will read the value from System.in and then store the data into input. int x= scanner.nextInt();. Programmer declared the x as integer and the scanner.nextInt(); is to let user to input a value.

if(x==1)

{

System.out.println("Using while loop:");

int a=1;

while(a<6)

{

int b=1;

while(b<=a)

{

System.out.print("* ");

b++;

}

System.out.println("");

a++;

}

}

If the user is choose the choice one and then the programs will loop above of the code. The program will display out as using while loop. Int a=1; is mean that the variable of 'a' is declared as integer and set the integer an initial value as 1. While (a<6) is mean that the while loop will evaluate the expression first and it mean that the integer a must less than 6. This is to tell the while loop will loop how many times. Besides that, declared a new variable as 'b' and also set the initial value as 1. While (b<=a) is means that the variable b is less than or equal to 5 only. If the expression is true than the while loop will execute the statement and will display out the star. If the expression is true then it will loop less than 6 times.

else if(x==2)

{

System.out.println("Using for loop :");

int m=1;

for(m=1;m<6;m++)

{

int l=1;

for(l=1;l<=m;l++)

{

System.out.print("* ");

}

System.out.println("");

}

}

If the user is chose choice two, the program will run for loop. First, the program will display out as using for loop and after that it will run for loop. Programmer declared m as integer and set the initial value as 1. For (m=1; m<6; m++) is mean that the initial value of m is 1, m is less than 6 and then it will do increment. Inside for loop contain another for loop which is known as nested for loop. Just now that loop is telling the program to loop how many times only. After that, declared another integer again. Int l=1; is mean that the integer l's initial value is 1. For (l=1; 1<=m; l++) is mean that l's initial value is 1, l is less than or equal to m and after that l will do increment. After that, it will display out the symbol of star.

else

{

System.out.println("You are key in a wrong input ");

}

If the user chose other choice then the program will display out as you are key in a wrong input as at above.

int p;

for(p=1;p>0;p++)

{

System.out.print ("Do you wish to continue?[Y/N] : ");

char choice = scanner.next().charAt(0);

if(choice=='Y' || choice=='y')

{

main(args);

}

else if(choice=='N' || choice=='n')

{

System.exit(0);

}

else

{

System.out.println ("you are key in a wrong input,please try key in again.");

}

{

System.out.println ("");

}

}

}

}

After that, programmer will create for loop for the statement that ask user whether wan continue or not. First, declared the p as integer. For (p=1; p>0;p++) is mean that initial value of p is 1, p is greater than 0 and after that it will do increment. System.out.println ("Do you wish to continue? [Y/N] : ") this code is display the output as Do you wish to continue? [Y/N] :, it is to ask user whether wan continue or stop to loop. After that, declared choice as char and this variable is to give user to key in character so put scanner.next().charAt(0); charAt(0) is if user type so type so many character, then the scanner will only take first position's character.

if(choice=='Y' || choice=='y')

{

main(args);

}

If(choice=="Y" || choice=="y") is mean that whether user is type smaller case of y or capital of y, it also is acceptable and then it will return to public static void main(String[] args).

else if (choice=='N' || choice=='n')

{

System.exit(0);

}

If the user is type small case of n or capital of n then the program will terminate by the code of System.exit(0).

else

{

System.out.println ("you are key in a wrong input, please try key in again.");

}

If user not type Y/N or y/n then the program will display out as you are key in a wrong input, please try key in again. Below is the result of program.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (5).bmp

Figure 1.1

First, the program will ask user to enter his choice to choose 1 for while loop or choose choice 2 is for loop.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (6).bmp

Figure 1.2

If the user is choose choice 1 then the program will show out the while loop. After that, the program will ask the user whether wan continue or not. If want continue then press Y , if don't want then press N.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (7).bmp

Figure 1.3

If the user type Y then the program will go back to the main and loop back again. It will ask the same question to the user.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (8).bmp

Figure 1.4

If the user type 2, the program will display out the for loop program.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (9).bmp

Figure 1.5

If the user has type N, program will be terminated.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (10).bmp

Figure 1.6

If the user is enter 3, then the program will display out the output as you are key in a wrong input. After that, the program will ask the user again whether want to continue or not.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (12).bmp

Figure 1.7

If the user is type other character, then the program will display out the output as you are key in a wrong input, please try key in again. After that, the program will ask back the user whether want to continue or not.

Answer question 2

import java.util.Scanner;

public class AssignmentQuestion2

{

public static void main(String[]args)

{

System.out.println ("Month\tStatement");

System.out.println ("1\t\tJanuary");

System.out.println ("2\t\tFebuary");

System.out.println ("3\t\tMarch");

System.out.println ("4\t\tApril");

System.out.println ("5\t\tMay");

System.out.println ("6\t\tJune");

System.out.println ("7\t\tJuly");

System.out.println ("8\t\tAugust");

System.out.println ("9\t\tSeptember");

System.out.println ("10\t\tOctober");

System.out.println ("11\t\tNovember");

System.out.println ("12\t\tDecember");

Scanner input = new Scanner(System.in);

System.out.println (");

System.out.println ("Enter a number to select month :");

int month = input.nextInt();

switch(month)

{

case 1: System.out.println ("January");

break;

case 2: System.out.println ("Febuary");

break;

case 3: System.out.println ("March");

break;

case 4: System.out.println ("April");

break;

case 5: System.out.println ("May");

break;

case 6: System.out.println ("June");

break;

case 7: System.out.println ("July");

break;

case 8: System.out.println ("August");

break;

case 9: System.out.println ("September");

break;

case 10: System.out.println ("October");

break;

case 11: System.out.println ("November");

break;

case 12: System.out.println ("December");

break;

default:System.out.println ("Invalid input");

}

}

}

Above of the code is require user to input a number to select the month. First, the code is contains import java.util.Scanner. This code is for user to enter input. If the program is require user to key in any input and the program is require the code of import java.util.Scanner. Public class AssignmentQuestion2 is to start a class for the AssignmentQuestion2. Next is about the public static void main (String[] args). This part of code is to start the execution of Java Program.

Next is about the code of System.out.println("Month\tStatement");. System.out.println in Java programming is mean of output. It will display what the user type in the bracket with double quotation. Above the code will display out the output of Month Statement. After that, put a semicolon. Semicolon is to indicate end of statement. After that, the code will display out the following output of month.

Next is about the code of Scanner input=new Scanner(System.in);. Scanner will read the data from System.in and store the data into the input.

Next is about the code of System.out.println("Enter a number to select month :");. This output is display out to call user to enter a number to select month.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture.bmpAbove is the output of that will display out that require user to enter a number to select the month.

Next is about the code of int month=input.nextInt();. This is declare the month is integer and equal to input.nextInt() is mean the user type what integer then the program will take the value. Next is about the switch statement. There are contain twelve case are because of one year consists of twelve month and last is default. Default is means that if the user has key in wrong the input then will pop up an invalid input that tell the user has enter wrong input. Each case contains of one break statement and break statement is to break each of the cases.

Below is the result of the program.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (3).bmp

The program will display out the output and require user to select a month to display out the statement.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (2).bmp

From the above, the user has type 6 and the output has display out as JUNE. After that, the program will terminate.

If the user is type out the value and the value is of 12 and not inside the month, then the output will display out as invalid input. Below is the example.

C:\Users\Joshua\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Word\New Picture (1).bmp

Conclusion

For the question one, finally the code has completely already and the whole program can run. After this, I have learned many thing and syntax in this question. This code has make me learn more about the looping like inside a loop still can put a or more loop. This question is full of challenging, but after completes the code I feel that I have learned many thing. I have learned how to write a code to print out the output, how to write a code to let user to enter a value, I also have learned about if...else statement to let user know if user type wrong then will display another output to show that user are enter a wrong value.

I also have learned about while loop and for loop. The loop is can loop many time until programmer has give a limit to it. Otherwise, it also can be loop with unlimited time. I also have learned how to arrange the symbol of star. Besides that, I also have learned how to end the program or return to the main.

For the question two, the code has been complete by successfully and the program can run. In the process of writing this code, I have learned many thing that about switch statement. The code also contains input function because question require user to input a value. Inside the switch statement, I also have learned many things. The switch statement has a break statement which is break each of the case. The switch statement of case is to declare that user can contain how many choices to type the value to display out the output. In switch statement, I also have learned that the difference between if...else and switch statement. If the statement is true then it will display out the output or else it will display out an output that show to user that him or her is enter a wrong value. But for switch statement, if the statement is true then the output will display same as the case's output. If not, then the program will go to default to display out the output that show to user that he/she is enter a wrong value.

Writing Services

Essay Writing
Service

Find out how the very best essay writing service can help you accomplish more and achieve higher marks today.

Assignment Writing Service

From complicated assignments to tricky tasks, our experts can tackle virtually any question thrown at them.

Dissertation Writing Service

A dissertation (also known as a thesis or research project) is probably the most important piece of work for any student! From full dissertations to individual chapters, we’re on hand to support you.

Coursework Writing Service

Our expert qualified writers can help you get your coursework right first time, every time.

Dissertation Proposal Service

The first step to completing a dissertation is to create a proposal that talks about what you wish to do. Our experts can design suitable methodologies - perfect to help you get started with a dissertation.

Report Writing
Service

Reports for any audience. Perfectly structured, professionally written, and tailored to suit your exact requirements.

Essay Skeleton Answer Service

If you’re just looking for some help to get started on an essay, our outline service provides you with a perfect essay plan.

Marking & Proofreading Service

Not sure if your work is hitting the mark? Struggling to get feedback from your lecturer? Our premium marking service was created just for you - get the feedback you deserve now.

Exam Revision
Service

Exams can be one of the most stressful experiences you’ll ever have! Revision is key, and we’re here to help. With custom created revision notes and exam answers, you’ll never feel underprepared again.