class Average { private int total; private int count; //constructor to ...

Software and s/w Development

Nov 3, 2013 (5 years and 2 months ago)

493 views

class

Average {

private

int

total
;

private

int

count
;

//constructor to
initialise

variables

public

Average(){

total
=0;

count
=0;

}

//addition of a number to the total

public

void

int

num){

total

+= num;

count
++;

}

//output the
average of all numbers

public

int

getAverage(){

return

total
/
count
;

}

}

class

TestAverage {

public

static

void

main (String[]args){

Average average =
new

Average();

Scanner scan =
new

Scanner(System.
in
);

for a number & whether to continue. When Q
is entered, the loop ends.

do
{

System.
out
.print(
"Enter Number:"
);

System.
out
.print(
"Type Q to quit or C to continue."
);

}
while

(!scan.next().equalsIgnoreCase(
"Q"
));

//se
lection if statement to print out whether the average is higher
or lower than 20.

if
(average.getAverage()>=20){

System.
out
.print(
"Average more than or equal to 20."
);

}

else
{

System.
out
.println(
"Average less than 20."
);

}

//prints out

average.

System.
out
.println(
"Average is: "

+ average.getAverage());

}

}

public

class

NumbersList {

public

static

void

main(String[] args)
throws

Exception{

//Declaring an Array

//int[] numbers = new int[10];

//Declaring an

Array List

ArrayList <Integer> numbers =
new

ArrayList <Integer>();

//Scanning a file

try
{

Scanner scan =
new

Scanner(
new

File(
"numbers.txt"
));

int

total = 0;

//while loop to scan through file & add to array

while

(scan.hasNextInt()){

}

//for loop

for
(
int

i = 0; i < numbers.size(); i++){

System.
out
.println(numbers.get(i));

total = numbers.get(i) + total;

}

//for each loop

for
(
int

i : numbers){

System.
out
.println(i
);

total = i + total;

}

System.
out
.print(
"Total: "

+ total);

}

catch
(FileNotFoundException e){

System.
err
.println(
"No file found"
);

}

}

}

public

class

WritetoFile {

public

static

void

main(String[] args)
throws

Exception {

PrintWriter writeFileOut =
null
;

writeFileOut =
new

PrintWriter(
new

BufferedWriter(
new

FileWriter
(
"file.txt"
,
true
) ) );

writeFileOut.println(
"This is line 1"
);

writeFileOut.println(
"This is line 2"
);

writeFileOut.println(
"This is line 3"
)
;

System.
out
.println(
"File has been saved."
);

writeFileOut.close();

//FileNotFoundException

//NoSuchElementException

//InputMismatchException

//ArrayIndexOutOfBoundsException

}

}

public

class

GUI {

private

JLabel
lblName
;

private

JTextField
txtName
;

private

JButton
btnUpper
;

private

JPanel
panel
;

private

JFrame
frame
;

public

GUI(){

createForm();

frame
panel
);

frame
.setVisible(
true
);

}

public

void

createForm(){

frame

=
new

JFrame(
"Uppercase Format"
);

frame
.setSize(650,125);

frame
.setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE
);

panel

=
new

JPanel();

panel
.setLayout(
null
);

}

public

void

lblName

=
new

JLabel(
"Input Text:"
);

lblName
.setBounds(10, 35, 100,
20);

panel
lblName
);

txtName

=
new

JTextField();

txtName
.setBounds(100, 20, 400, 50);

panel
txtName
);

}

public

void

btnUpper

=
new

JButton (
"UpperCase"
);

btnUpper
.setBounds(520, 20, 100, 50);

btnUpper
new

UpperCaseHandler());

panel
btnUpper
);

}

class

UpperCaseHandler
implements

ActionListener {

public

void

actionPerformed(ActionEvent event) {

txtName
.setText(
txtName
.getText().toUpperCase());

}

}

public

static

void

main(String[] args){

new

GUI();

}

}