Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,587 members, 7,809,124 topics. Date: Friday, 26 April 2024 at 12:14 AM

Java Code Improvement. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Code Improvement. (671 Views)

Please Help With Java Code For This Simple App.. / Need Assistance With A Java Code / Improve The Speed Of This Java Code. (2) (3) (4)

(1) (Reply)

Java Code Improvement. by Nobody: 6:34pm On Jan 18, 2013
Hello everyone.

I'm learning Java (albeit, in University), and was tasked with writing some program which takes a file, and utilizes StringTokenizer, cleans it up and prints things back to the user, i.e., name, age, et al.

So, the second bit of the task is to make routines maxAge(), minAge(), and avgAge(), all of which I've done shown here: http:///l1CnJ

It's sloppy, and can certainly be done better, but I'm not sure how. Each class has methods, and the Person.java class has methods that are integrated into the constructors, i.e., Person.getAge(), and the maxAge() methods are done using static variables, I feel it can be done better and in a new class, accepting a List <Person> as an argument, but even that seems a bit sketchy.

The next bit of the task is to make the routines faster, but I think I've done it all so badly that I may need to rewrite.

Could anyone point me in the right direction?

(Literally been programming in Java for less than a month.)
Re: Java Code Improvement. by naijaswag1: 6:56pm On Jan 18, 2013
That link brings up nothing but a server not found on mozila firefox. Paste the code here on Nairaland. However,this is not an algorithmic challenge where you are concerned with the speed and efficiency of your code. Organize your code in an OOP oriented way and get the job done.
Re: Java Code Improvement. by Nobody: 7:16pm On Jan 18, 2013
naija_swag: That link brings up nothing but a server not found on mozila firefox. Paste the code here on Nairaland. However,this is not an algorithmic challenge where you are concerned with the speed and efficiency of your code. Organize your code in an OOP oriented way and get the job done.


It's quite a bit of code, separated by different source files. The one in question is Person.java

package labs_one;

public class Person {
static int age, max, min;
static int first, count, sum;
static boolean check = true;

String firstname;
String lastname;

@SuppressWarnings("unused"wink private Person() {}

public Person(String fn, String ln, String a) {
firstname = fn;
// Make all last names uppercase.
lastname = ln.toUpperCase();

// We're very likely to get whitespace. Let us clear that up.
age = Integer.parseInt(a.trim());
}

public int maxAge() {
if (age > max) max = age;
return max;
}

public int minAge() {
count++;
sum = sum + age;

// We need to get the very first value, and make sure we only handle getting it once.
// Quick hack to do this. Is there a better way?
while (check == true) {
first = age;
min = first;
check = false;
}

if (age < min) min = age;
return min;
}

public double avgAge() {
return ((double) sum / count);
}

public String toString() {
return ("Name: " + firstname + " " + lastname + " " + "Age: " + age);
}
}


I don't know how better to object orient it smiley

(1) (Reply)

Php / Data Base Management / You Need A Website? Get It Now, Pay Later!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.