In: Computer Science

Write a complete Java program, including comments in both the main program and in each method, which will do the following:

0. The main program starts by calling
a method named **introduction** which prints out a
description of what the program will do. This method is called just
once.

This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the program does (it should also say how to end the set of data—see step 4).

1. Then the main program asks the user to type in an integer value which the main program calls n; n could be positive, negative, or zero (see step 4). The main program prints the number n after it is read in. Use a negative value to end.

2. The main program calls a method
named **isiteven,** sending it the integer value n as
a parameter. The method determines whether or not n is even,
sending the answer back to main. (Hint: if n is even, the remainder
when n is divided by 2 is 0; this works for all even numbers,
including 0.) You may return an integer (like 0 or 1) to represent
even or odd, or return a character (like ‘e’ or ‘o’), or a
boolean.

The main program prints the answer returned together with a message. For example, if you send 6, the method determines it is even, and the main program prints that 6 is an even number.

3a. If n is even, the main program
calls a method named **sumEvenSquares**, sending n to
the method as a parameter. The method computes the sum of the first
n even squares (see below). The method returns this sum to the main
program. Then the main program prints a message giving n and the
sum of the first n even squares.

For example, if you send 4, then the sum of the first 4 even squares is 2*2 + 4*4 + 6*6 + 8*8 = 4+16+36+64 = 120.

Note the method computes the sum of the first n even squares- this will be: 2*2 + 4*4 + 6*6 + ... + (2n)*(2n), where n is the parameter value sent to the method. DO NOT use any other formula for calculating this value; use the sum of series.

b. However, if n is odd, the main
program calls a method named **sumOddNumbers**,
sending it n. The method computes the sum of the first n odd
numbers (NOT n odd squares - see below). The method returns this
sum to the main program. Then the main program prints a message
giving n and the sum of the first n odd numbers.

For example, if you send 3, then the sum of the first 3 odd numbers is 1 + 3 + 5 = 9. For n=5, it is 1+3+5+7+9 = 25, etc. (Although this value will be equal to n squared, calculate it as a series; don’t just find n squared.)

Note the method computes the sum of the first n odd numbers- this
will be: 1 + 3 + 5 + ... + (2n-1), where n is the parameter value
sent to the method.

In either case, the main program calls one of two methods to compute a sum of terms, then prints the method's answer.

4. After calling a method to compute the appropriate sum and printing the result, the main program will skip a few lines and go back to step 1 (not step 0). At step 1, if the user types in a negative value, the program will go to step 5.

5. At the end, print how many data values were entered and processed. (Make sure that this number is at least 8.)

**DATA**: Type in a total
of at least 8 data values. Have at least four even numbers (make
sure 0 is one of the even numbers) and four odd numbers;
intersperse the values: odd, then 2 evens, then odd, etc. Have one
value of each type that is between 10 and 20 (and the rest
smaller).

** You
will be judged on the quality of your data.**

**STYLE**: Be sure that
each method has a good comment explaining two things: exactly what
parameter(s) the method will receive, and exactly what the method
will do (and if it returns an answer or prints). Mention parameters
by name in the comment.

**OUTPUT:** Send output
to an external file. You will need to pass it as a parameter.

Here is some sample output (ignoring the introduction):

the original integer is 5

5 is an odd number

the sum of the first 5 odd numbers is 25

the original integer is 4

4 is an even number

the sum of the first 4 even numbers is 120

**OPTIONAL**: If the user
enters a negative value, say this is no good, ask the user to type
in a new value that is good (greater than or equal to 0). The
program continues until the user enters a special value (not just
any negative value) that signals the end.

The output filename is passed as an command line argument

..the program could be run from terminal as **java
Javaprogram.java file_name.txt.... **

Or for netbeans IDE go to Run tab and then project configuration and then wirte the filename in agrument field in dialog box opened and then run the program

**The java code snipped and screen shots has been attached
below**

```
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaprogram;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author vijay
*/
public class Javaprogram {
/**
* @param args the command line arguments
*/
static BufferedWriter out; //object for writing to the file
//this function has no parameter and has void return type..it simply prints the introduction of program
public static void introduction() throws IOException //this function prints the introduction as given in question
{
out.write("Name : vijay \n The program is to take a input of integer "
+ "\nIf the input is -1(special integer) then program will terminate else \n If the "
+ "input is positive then if it is even then it is displayed at the main that it is even "
+ "and then sum of even squares upto that even number is calculated \n...If"
+ "the number is odd then then again it is notifed and sum of first n odd numbers is calculated "
+ " if the number is negative then it is asked to enter the another good value\n\n " );
}
//this function accept an integer parameter and returns whether the passed paramenter is even or odd
public static char isiteven(int n)//this function check whether the number is even or dd
{
if(n%2==0) //if remainder is 0 it is even
return 'e'; //return 'e' which means even
else
return 'o';//return 'o' which means od
}
//this function accepts an integer parameter n and returns the integer which is sum of first n even squares
public static int sumEvenSquares(int n) //this function calculates the sum of first n even squares
{
int sum=0; //intialising the sum to 0
for(int i=2;i<=2*n;i+=2) //traversing the loop from 2 to 2*n ..at an update of 2
{
sum+=i*i; //adding i*i to sum variable
}
return sum; //returning the calculated sum
}
//this function accepts an integer parameter n and returns the integer which is sum of first n odd numbers
public static int sumOddNumbers(int n)
{
int sum=0; //intialising the sum to 0
for(int i=1;i<=2*n-1;i+=2)//traversing the loop from 1 to 2*n-1 ..at an update of 2
{
sum+=i; //adding i to sum variable
}
return sum; //returning the calculated sum
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
String Filename=args[0];//taking file name as command line arguement
FileWriter fout=new FileWriter(Filename); //creating FileWriter object to write on the file
out=new BufferedWriter(fout); //intialising BufferedWriter object by passing object of FileWriter
Scanner sc =new Scanner(System.in); //creating object of scanner class to take the inputs
introduction(); //prints the introduction of the program
int count=0 ; //count the number of inputs processed
while(true) //making a while loop to return the below statements for couple of times
{
System.out.println("Enter any integer...(-1 to terminate the program) \n");
int n=sc.nextInt();//taking the input from stdin
if(n==-1) //checking condition for termination
{
break; //break the loop if n=-1
}
count ++;
out.write("The orignal integer is "+n+"\n");
char typeOfInteger=isiteven(n);//calls isiteven method to check whether number is even or odd
if(typeOfInteger=='e') //checking if number is even
{
out.write(n + " is an even number \n");//printing the number is even
int sum=sumEvenSquares(n);//calling sumEvenSquares function to calculate the sum of first n even squares
out.write("The sum of first "+n +" even squares is "+sum+"\n");//printing the required sum
}
else
{
out.write(n + " is an odd number\n");//printing the number is odd
int sum=sumOddNumbers(n);//calling sumOddNumbers function to calculate the sum of first n odd numbers
out.write("The sum of first "+n +" odd numbers is "+sum+"\n");//printing the required sum
}
}
out.write("The total data values eneterd and processed are :"+ count);//wirte the total input processed to the file
out.close();//closing the stream
}
}
```

**Screenshot**

Write a complete Java program,
including comments in both the main program and in each method,
which will do the following:
0. The main program starts by calling
a method named introduction which prints out a
description of what the program will do. This method is called just
once.
This method
is not sent any parameters, and it does not return a value. The
method should print your name. Then it prints several lines of
output explaining what the...

Write a complete Java program, including comments in each method
and in the main program, to do the following:
Outline: The main program will read in a group
of three integer values which represent a student's SAT scores. The
main program will call a method to determine if these three scores
are all valid--valid means in the range from 200 to 800, including
both end points, and is a multiple of 10 (ends in a 0).
If the scores are...

Please write a complete C coding program (NOT C++) that has the
following: (including comments)
- declares two local integers x and y
- defines a global structure containing two pointers (xptr,
yptr) and an integer (z)
- declares a variable (mst) by the type of previous structure
- requests the values of x and y from the user using only one scanf
statement
- sets the first pointer in the struct to point to x
- sets the second...

Write a complete program in java that will do the following:Sports:
Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:
9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball 9
players.Basketball 5 players.Football
11 players.Hockey
6
players.Volleyball 6 players.Waterpolo 7 players.Use Scanner to provide the number of friends you have for a
team sport.Provide an output of suggested sports for your group of
friends. If your...

Hello,
Please write this program in java and include a lot of comments
and please try to make it as simple/descriptive as possible since I
am also learning how to code. The instructions the professor gave
was:
Create your own class
Your own class can be anything you want
Must have 3 instance variables
1 constructor variable → set the instance
variables
1 method that does something useful in relation to the
class
Create a driver class that creates an...

write the program in java.
Develop a class RentCabin that does the following: (use JavaDoc
comments)
// declare the constructor that sets the type and rate based in
the sqft parm
// set values based on sqft
<1000 is small with $100 per night,
// sqft between 1000 and 2000, mid-sized $200 per night, and
// over 2000 as a large cabin with $300 per night
//declare getRate
//declare getType
//declare setRate with int
rate parm...

Language: Java
Create a TryIt.java program with a main method. You are going to
use this
program to demonstrate some things about how Java works. Make your
methods
here private, because they are not intended to be called from
outside the program.
For each test below, make sure that you print to the console:
1) What is being tested
2) The desired output
3) The actual output
Question to be answered: Should you use == or the String method
equals...

(a) In C++, write down the main method including a for loop. The
body of the for loop includes the followings :
i. an if condition
ii. an array (of any type & of any size)
iii. a structure variable (with at least two variables of any
type)
(b) Show the corresponding code contents in memory for the main
method and draw the stack contents while the main method starts
execution and reaches at the line prior to return from...

Write a program named MyHometown_Icon.java. The program will be
an application (i.e have a main method). It will be in the default
package. It will import edu.wiu.StdDraw. Its main method will make
calls to methods in StdDraw to draw something iconic about your
hometown.
Multiple colors should be used.Multiple primitive types will be
used and the drawing will consists of more than 20 primitive
shapes.

QUESTION
Write the main while loop for a Java program that processes a
set of data as follows:
Each line of the input consists of 2 numbers representing a
quantity and price. Your loop should:
1. Read in the input
2. Calculate the tax. Tax will be 8.875% of price and will only
be applicable if price is more than 110. Calculate the new price
which is the price plus tax.
3. Calculate the final price by multiplying quantity by...

ADVERTISEMENT

ADVERTISEMENT

Latest Questions

- What are some reasons why household wealth might fluctuate even if current income is unaffected? Give...
- Data on the fuel economy of several 2010 model vehicles are given in the accompanying table....
- Let's use 32 as the number of ATP produced per glucose's aerobic catabolism. (See the last...
- If money is invested for 4 years, the per annum simple interest rate equivalent to a...
- There are seven photocopiers, and three of these photocopiers are defective. The buyer begins to test...
- $1,000 is invested at 5%p.a compounding semi-annually for the first 3 years and at 11% p.a...
- Select a journal article that discusses a project to improve patient safety. Review the article and...

ADVERTISEMENT