FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Java project coupling problems





Dark-Tech
I am being forced to create a multi class specificly 3 class project in java that does a certain thing described in Image 1

I have managed to create a working code that completes the task but unfortunately the teacher won't accept it because its only in one class

so im trying to turn it into three classes.

i have included here the 1st code and the three codes ive managed to derive from it

i have also inserted a link to where you can download the project/ classes they were created with BlueJ

pls help




The one Class code

import javax.swing.JOptionPane;
public class barcodewhole
{

public static void main(String [] args)
{
int zip;
String zipcode = JOptionPane.showInputDialog("insert zipcode");
int x = 0;
int correct = 0;
String bar;
while (x < 5)
{
char ch = zipcode.charAt(x);
int i = (int) ch;
String zips = new Character((char)i).toString();
zip = Integer.parseInt(zips);
if (zip == 1)
{
bar = ":::||";
}
else if (zip == 2)
{
bar = "::|:|";
}
else if (zip == 3)
{
bar = "::||:";
}
else if (zip == 4)
{
bar = ":|::|";
}
else if (zip == 5)
{
bar = ":|:|:";
}
else if (zip == 6)
{
bar = ":||::";
}
else if (zip == 7)
{
bar = "|:::|";
}
else if (zip == 8)
{
bar = "|::|:";
}
else if (zip == 9)
{
bar = "|:|::";
}
else if (zip == 0)
{
bar = "||:::";
}
else
{
bar = "ERROR";
}
System.out.print(bar);
x++;
correct = correct + zip;
}



if (correct%10 <= 5 || correct < 10)
{
zip = (correct%10);
}
else
{
zip = (Math.abs((correct%10)-10));
}


if (zip == 1)
{
bar = ":::||";
}
else if (zip == 2)
{
bar = "::|:|";
}
else if (zip == 3)
{
bar = "::||:";
}
else if (zip == 4)
{
bar = ":|::|";
}
else if (zip == 5)
{
bar = ":|:|:";
}
else if (zip == 6)
{
bar = ":||::";
}
else if (zip == 7)
{
bar = "|:::|";
}
else if (zip == 8)
{
bar = "|:|::";
}
else if (zip == 0)
{
bar = "||:::";
}
else
{
bar = "ERROR";
}
System.out.print(bar);
}
}


The 1st derived class digit

public class digit
{
//Parameters: a sString
//precondition: a all numeric String created by the user containing only a zipcode is n to the method
//Post: returns an integer that is ment to go to the barcode class.
public static int digit2(String zipcode, int q)
{
int zip;
int x = 0;
int correct = 0;
String bar;
if (q < 5)
{
char ch = zipcode.charAt(q);
int i = (int) ch;
String zips = new Character((char)i).toString();
zip = Integer.parseInt(zips);
x++;
correct = correct + zip;
return zip;
}

if (correct%10 <= 5 || correct < 10)
{
zip = (correct%10);
}
else
{
zip = (Math.abs((correct%10)-10));
}
return zip;
}
}


The secound deived class

public class barcode
{
//Parameters: a single digit positive integer
//precondition: an integer is sent to the barcode
//Post: returns a string that containms the barcode for that integer
public static String barcode2(int x)
{
String bar;
if (x == 1)
{
bar = ":::||";
}
else if (x == 2)
{
bar = "::|:|";
}
else if (x == 3)
{
bar = "::||:";
}
else if (x == 4)
{
bar = ":|::|";
}
else if (x == 5)
{
bar = ":|:|:";
}
else if (x == 6)
{
bar = ":||::";
}
else if (x == 7)
{
bar = "|:::|";
}
else if (x == 8)
{
bar = "|:|::";
}
else if (x == 0)
{
bar = "||:::";
}
else
{
bar = "ERROR";
}
return bar;
}
}


The Third class, test class

import javax.swing.JOptionPane;
public class bartest
{
/**
calls for the creation of a distance object all onditions are inherited
**/

public static void main(String [] args)
{

String zipcode = JOptionPane.showInputDialog("insert zipcode");
int q = 0;
int cor = 0;
System.out.println("|");
if (zipcode.length() ==5)
{
System.out.println("|");
while (q <= 5)
{
int zip=digit.digit2(zipcode, q);

String bar = barcode.barcode2(zip);
System.out.println(bar);
q++;
}
System.out.println("|");
}
else
{
System.out.println("Error");
}
}


}


The link to the folder containing the project files
http://www.cyberneticrisis.frihost.net/down/java/
kv
So, you have written the classes, and want us to help without telling what the problem is? I don't have the patience to go through all the code and guess the problem. If you can tell us what exactly you are looking for, we might try to help.
Dark-Tech
there were quite a few problems with it like the reinitializing of a variable to zero every time a calss would be called
so i just tweaked the classes so they're not entirely the way i think they were supposed to be but i think i can get a way eith it seeing as it works


now my problems is i have to create another 3 class code that turs the barcode back into the zipcode but ill probly be able to figure that out seeing as i have something that works to work with now
elekis
one ting sure
do not use

if (zip == 1)
{
bar = "::Neutral|";
}
else if (zip == 2)
{
bar = ":NeutralNeutral";

thats stupid and illisible prefer use a double array where withc enter equals a string .


now question, qhy three class ??

I mean, if I really understand you ve just have a int on intput and a string on output.

why make three class, it's even not objet. ?? or maybe I don't understand the prob.

intput << INT a
vect[10][2] Z
string c
while(a != 0)
b = a%10
c += Z[b][2]
}
output >> c


I mean, really, why ONE class???(ok it's java , but...)






a+++
Dark-Tech
idk the iunstructor said we had to do it in three classes no more no less thats what messed me up but im doneyso i really dont need to an anser i just made it so it looked good for three classes.
elekis
I think i understood something??

if you needs three classes, it's because two must inherite from the bases class. right ??

so the first must be abstract (or have abstract methodes.) and I don't see your abstract. second where your extends.(in derived class)???

but it's if I understood your project (and I m not shure)???


a++++
nunosilva
hey, wouldn't it be better for you to use case instead of if clauses? I think case is a bit faster, even with the code I've done to check a value you insert, I did
switch(x) {
case 1: System.out.println("um"); break;
...
...
...
...
default: System.out.println("nao existe"); break;
}

this code, when run gave me this running times:

~/Java time java Teste 4
quatro

real 0m0.068s
user 0m0.052s
sys 0m0.009s

for a code using if clause, doing the same line of thought, this is, using your way... took:

~/Java time java Teste2 4
quatro

real 0m0.117s
user 0m0.057s
sys 0m0.003s

well, that's a diference, right?
As the rest of code... it's too long to go line by line, and there's a huge fly here in the room making so much noise, that I can't focus... I'm gonna kill it!!!!

ARGGGGGHHHH
Related topics
Problems with Java
(official) IE 7 Topic
Java download question
Java tutorials
[Java] Top-Down Menu bar tweak.
interfaces in JAVA
Windows Movie Maker problems.
Explain your current avatar!
JAVA Trouble
some one give me a guide??
Java
Sports Arbitrage Betting Project - Developers Needed!
Eclipse running on Windows CE or Pocket PC
Ant Build
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.