Você está na página 1de 3

Variables and User Input

In order to make a program useful we have to allow the user to enter information that affects the program flow and
outcome of the system and to do this we have to use variables. Variables allow us to store information within the
program so that we can perform calculations and store the information provided.

Type the following program. Save it and then run it. Fix any errors you encounter.

(When a line of code crosses over two lines because the page isnt wide enough then type it on one line in your java
compiler)
/***************************************************************
ProgramName : Area of a Rectangle
Author : Your Name
Date : Todays Date
Calculates the area of a given rectangle and displays the result to screen
/***************************************************************/

public class AreaOfARectangle
{
public static void main(String args[])
{
//Assigns the length and breadth from the programmer and displays the result to screen

int length=5, breadth=10, area;

System.out.println("This program will calculate the area of a rectangle\n");
System.out.println("------------------------------------------------------------\n\n");
System.out.println("The Length is 5 metres and the Breadth is 10 metres\n\n");
area = (length * breadth);
System.out.println("The area of the rectangle is " + area + " m2\n");

}
}
..

This time we will allow the user to enter the length and the breadth of the rectangle. Type in the
following.

int length, breadth, area;

System.out.println("This program will calculate the area of a rectangle\n");
System.out.println("------------------------------------------------------------\n\n");
System.out.println("All input should be entered in metres\n\n");

System.out.print("Please Enter the Length of the rectangle : ");

length = scanner.nextInt();

System.out.print("Please Enter the Breadth of the rectangle : ");

breadth = scanner.nextInt();

area = (length * breadth);

System.out.println("\nThe area of the rectangle is " + area + " m2\n");


Variable types
In Java there are numerous basic pre-defined data types including

Type Meaning Storage Min Value Max Value
Numeric Values
int Integer 32 bits -2,147,483,648 2,147,483,647
short Short int 16 bits -32,767 32,767
float Floating point
number
32 bits -3.4E+38 3.4E+38
double Large floating
point number
64 bits -1.7E+308 1.7E+308
byte Byte 8 bits -128 127

char Character 16 bits
String String
boolean Boolean true False
.
Initialising variables
A variable must be given a value before it can be of any use to us and this can be done either by the user entering
a value from a prompt or by giving it a value from within the code (either at the declaration stage or within the
code itself).
int length =4; defines an integer variable with the value of 4 as it is declared.
Length = 4; sets the value of the variable during the execution of the code.

Numeric Variables
When using numeric values within your code you must decide on the most appropriate type of variable to suit
your number. An incorrect choice of variable type can have surprising and sometimes catastrophic result on
your program.

Strings
Java lets us deal with Strings quite simply by allowing us to read them in as one complete value (The whole line
up till the return key has been pressed) therefore taking the burden off of us.

Boolean
A Boolean variable allows us to store whether something has a true or false value. This is a very useful variable
type and we will return to it later.



Programming Problems

Design and write programs that will solve the following problems (use comments and indentation).

1. A program is required to prompt the user to enter their first name. This is followed by a second prompt
asking the user to enter their surname. The program will then display both names to screen. Identify the
variable types before you begin.

2. Consider the following program

/***************************************************************
ProgramName : Program Name
Author : Your Name
Date : Todays Date
Displays Parkinsons Law to screen
/***************************************************************/

public class WHMParkinson
{
public static void main(String args[])
{/*Prints Parkinsons Law details to screen
System.out.println("Parkinsons Law\nWork expands so as to ");
System.out.println("fill the time"\n);
System.out.println("available for its completion.\n");
}

}

(a) On a piece of paper illustrate what output this program will produce.
(b) Identify the mistakes and the missing items in this program.

3. Adapt the program for problem 1 to accept a user number to go with their username. Both sets of input
should be displayed to screen. Identify the variable types before you begin.

4. Further adapt the program to accept five sets of values from the user. Once all values have been input the
details will be displayed to screen in the opposite order to which they were entered. Identify the variable
types before you begin.

5. Write a program that declares both integer and float variables and then displays them to screen without
them being initialised. Does the program compile and run? If not why not?. What would you need to do to
fix the program?

6. Create a program that will calculate and display the area of a circle from a radius of 10. (Area of a circle =
pi*r*r (pi = 3.14)).

Type the code below and run it, correcting any errors. Try to make the program interactive by allowing the user to
input the two number.
int firstNumber = 20;
int secondNumber = 10;
int result = 0;

result = firstNumber + secondNumber; // 100 + 20
System.out.println("Addition: " + result );
result = firstNumber - secondNumber; // 100 - 20
System.out.println("Subtraction : " + result );

result = firstNumber * secondNumber;; // 100 x 20
System.out.println("Multiplication : " + result );
result = firstNumber/secondNumber; // 100 20
System.out.println("Division sum: " + result );

Você também pode gostar