Be a Supporter!

Modularization? Pssshhh (java)

  • 552 Views
  • 23 Replies
New Topic Respond to this Topic
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Modularization? Pssshhh (java) 2012-08-01 22:09:06 Reply

I didn't feel like rewriting code. So i combined 4 sorts in one. This will sort an arraylist from highest to lowest, accessing the elements double values or the string values.

So basically i can sort the items in my grocerystore by price(lowest to highest, or highest to lowest), or alphabetically (A-Z or Z-A).

I am still working out some kinks to make it a bit more effeceint. I don't plan to change the method i am using to sort it though even though i am certain there are better ways.

Here is the code!

public ArrayList<Food> sort(ArrayList<Food> list, boolean high,boolean price)
    {
        int scope = list.size();
        for(int i = 0;i<scope;i++)       
        {           
            Food temp = list.get(i);
            int size = sortedList.size();
            boolean added = false;            
            if(size > 0)
            {               
                double price = temp.getPrice();        
                String name = temp.getName();
                outerloop:
                for(int j=0; j<size ;j++)
                {                   
                    Food foodTemp = sortedList.get(j);
                    double tempPrice = foodTemp.getPrice();
                    String tempName = foodTemp.getName();
                    if(high == true)
                    {               
                        if(price == true)
                        {
                            if( price > tempPrice)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }else{                        
                            if( name > tempName)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }
                    } else {        
                        if(price == true)
                        {
                            if( price < tempPrice)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }else{                        
                            if( name < tempName)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }
                    }
                }    
                if(added == false)
                {
                    sortedList.add(temp);
                }                
            }else{
                sortedList.add(temp);
            }
        }
        list.clear();
        return sortedList;
    }

Basically 4 functions jammed into one lol.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-01 22:24:52 Reply

Oh shit i forgot to modify the string comparison part. WOOPS :(

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-01 22:31:10 Reply

public ArrayList<Food> sort(ArrayList<Food> list, boolean high,boolean price)
    {
        int scope = list.size();
        for(int i = 0;i<scope;i++)       
        {           
            Food temp = list.get(i);
            int size = sortedList.size();
            boolean added = false;            
            if(size > 0)
            {               
                double price = temp.getPrice();        
                String name = temp.getName();
                outerloop:
                for(int j=0; j<size ;j++)
                {                   
                    Food foodTemp = sortedList.get(j);
                    double tempPrice = foodTemp.getPrice();
                    String tempName = foodTemp.getName();
                    if(high == true)
                    {               
                        if(price == true)
                        {
                            if( price > tempPrice)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }else{                        
                            if( name.compareTo(tempName) > 0)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }
                    } else {        
                        if(price == true)
                        {
                            if( price < tempPrice)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }else{                        
                            if( name.compareTo(tempName) < 0)
                            {                       
                                sortedList.add(j, temp);
                                added = true;
                                break outerloop;      
                            }
                        }
                    }
                }    
                if(added == false)
                {
                    sortedList.add(temp);
                }                
            }else{
                sortedList.add(temp);
            }
        }
        list.clear();
        return sortedList;
    }

I havn't tested it or debugged it for the string comparison. Their might be a logical error there might not. anyway laziness gets the best of me.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 01:03:55 Reply

I'm missing the point here.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 01:08:13 Reply

no point really, just bored.

nameistaken1
nameistaken1
  • Member since: May. 21, 2007
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Modularization? Pssshhh (java) 2012-08-02 09:33:39 Reply

Good thread.


BBS Signature
doodle-bread14
doodle-bread14
  • Member since: Jul. 25, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 09:36:56 Reply

At 8/2/12 09:33 AM, nameistaken1 wrote: Good thread.

Good thread indeed.
And Murphy, what is the use of that program above?


At least not a muffin.

BBS Signature
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 14:30:00 Reply

I asked my dad to give me a project to program for him to prepare me for school coming up since i decided to do art instead of programming all summer. He asked me to write him a virtual grocery store where he can create and organize a grocery list.

So i am giving him the option to sort the list and do searches to find particular items to put in and i am going to use prices from walmart for a general option to go by. Then i will have it so he will have multiple grocery lists and the ability to enforce a budget where it will or wont allow him to buy certain food options from my virtual store.

In later development i will incorporate a database system that will adapt according to how he buys food items, when he buys it and based off information from his reciets and the actual prices all the prices of the food items will change.

Or any other ideas i come up with.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 14:31:54 Reply

At 8/2/12 09:36 AM, doodle-bread14 wrote: Good thread indeed.
And Murphy, what is the use of that program above?

Also, its just one function. its not a program!

doodle-bread14
doodle-bread14
  • Member since: Jul. 25, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 14:40:21 Reply

So, it's not complete? How many functions are remaining? Or are there no more functions?


At least not a muffin.

BBS Signature
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 14:54:14 Reply

Right now i have about 6 classes. Some with 0 functions, some with 4, some with 1. So about 20 functions? i havn't even started to construct the GUI or input feed, i just finished my sorts, now i am going to do the searches.

Also, i am working on the design of the program in a IDE called BlueJ. It's not a real IDE its for educational purposes, the reason i like it is because as you create classes it shows a diagram so you can visualize everything. But it abstracts the main() method.

I've been working on it for 2 days. So the amount of functions and classes are still to be determined.

doodle-bread14
doodle-bread14
  • Member since: Jul. 25, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 15:07:17 Reply

At 8/2/12 02:54 PM, PMMurphy wrote: Also, i am working on the design of the program in a IDE called BlueJ.

Good luck with that!
One more question... why Java?


At least not a muffin.

BBS Signature
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 15:12:30 Reply

I've only been programming for 7 months its the only language i know. When i get better at programming i am going to start learning other languages.

NorskeDrittsekk
NorskeDrittsekk
  • Member since: Apr. 10, 2012
  • Offline.
Forum Stats
Member
Level 02
Musician
Response to Modularization? Pssshhh (java) 2012-08-02 15:31:03 Reply

At 8/2/12 02:54 PM, PMMurphy wrote: Also, i am working on the design of the program in a IDE called BlueJ.

High-IQ required for viewing BlueJ, may need cybernetic brain for interacting with 'the IDE'...

Why not use Eclipse or Netbeans?

srsly why use Java?
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 15:35:16 Reply

When working with designs it is good to have a visual diagram. That way you can visually see how the classes interact and work, if your just staring at code you cannot. Also i am using java because my cirriculum doesn't teach other languages. The purpose is to prepare me for school.

Why make life harder on yourself? I am going to take the design i make in BlueJ and write it in eclipse afterwards.

NorskeDrittsekk
NorskeDrittsekk
  • Member since: Apr. 10, 2012
  • Offline.
Forum Stats
Member
Level 02
Musician
Response to Modularization? Pssshhh (java) 2012-08-02 16:45:16 Reply

At 8/2/12 03:35 PM, PMMurphy wrote: Why make life harder on yourself? I am going to take the design i make in BlueJ and write it in eclipse afterwards.

It's all up to your own choices. I was just giving advise here.

Eclipse is a noob-friendly and true IDE. It literally corrects every syntax or logic bug I made. Think of it as a M$ Word for programmers. it supports not only Java but C, C++, COBOL, Haskell, Perl, PHP, Python (Django), R, Ruby (Rails), Scheme and more. Huge community support in case you get lost.

MonoDevelop is another good runner-up.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 17:11:32 Reply

At 8/2/12 04:45 PM, NorskeDrittsekk wrote: M$

How quaint. :)

He's right though; Eclipse is a damn fine IDE.
It's what I primarily use whenever I develop large and/or complicated PHP applications (though typically I use Notepad++ for that, but that's irrelevant).

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-02 18:32:02 Reply

At 8/2/12 04:45 PM, NorskeDrittsekk wrote: Eclipse is a noob-friendly and true IDE. It literally corrects every syntax or logic bug I made.

That's why i use Eclipse second.

doodle-bread14
doodle-bread14
  • Member since: Jul. 25, 2012
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Modularization? Pssshhh (java) 2012-08-02 18:45:38 Reply

At 8/2/12 06:32 PM, PMMurphy wrote: That's why i use Eclipse second.

Wise choise, I must say.


At least not a muffin.

BBS Signature
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-08-09 14:56:04 Reply

So i cleaned it up a bit. Made it look nicer and stuff. My goal was to repeat as little code as possible.

public ArrayList<Food> sort(ArrayList<Food> list, boolean high,boolean isPrice)
    {
        int scope = list.size();  
        for(int i = 0;i<scope;i++)       
        {         
            int size = sortedList.size();
            Food temp = list.get(i);
            boolean added = false;            
            if(size > 0 && isPrice)
            {       
                double price = temp.getPrice();
                added = sortedList(size,high,isPrice,price,"",temp,added);
            }else if (size > 0 && !isPrice)
            {
                String name = temp.getName();
                added = sortedList(size,high,isPrice,0.0,name,temp,added);
            }else if(size == 0)
            {
                sortedList.add(temp); 
                added = !added;
            }
            if(!added)
            {
                sortedList.add(temp);
            }
        }
        list.clear();
        return sortedList;
    }

    public boolean sortedList(int size,boolean high,boolean isPrice,double price,String name,Food temp,boolean added)
    {
            outerloop:
            for(int j=0; j<size ;j++)
            {                   
                Food foodTemp = sortedList.get(j);
                double tempPrice = foodTemp.getPrice();
                String tempName = foodTemp.getName();
                if((high && isPrice && (price > tempPrice)) || (high && !isPrice && (name.compareTo(tempName) > 0)) || (!high && isPrice && (price < tempPrice)) || (!high &&!isPrice && (name.compareTo(tempName) < 0)))
                {               
                    sortedList.add(j, temp);
                    added = !added;
                    break outerloop;
                }
            }   
            return added;
    }
EuPlonKa
EuPlonKa
  • Member since: Dec. 18, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Modularization? Pssshhh (java) 2012-08-13 01:05:22 Reply

At 8/9/12 02:56 PM, PMMurphy wrote: So i cleaned it up a bit. Made it look nicer and stuff. My goal was to repeat as little code as possible.

Have you considered implementing Comparable?
http://docs.oracle.com/javase/6/docs/api/java/lang/Comparabl e.html

kaiser-d
kaiser-d
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Modularization? Pssshhh (java) 2012-08-21 02:18:10 Reply

At 8/2/12 03:31 PM, NorskeDrittsekk wrote:
At 8/2/12 02:54 PM, PMMurphy wrote: Also, i am working on the design of the program in a IDE called BlueJ.
High-IQ required for viewing BlueJ, may need cybernetic brain for interacting with 'the IDE'...

Why not use Eclipse or Netbeans?

Yes. Use Netbeans. Seriously.

srsly why use Java?

Because Java gets you work. Its in higher demand than pretty much anything right now, and seeing as though it's the basis for android app development, it's probably going to stay that way for the forseeable future. Good on you.

Also, you ought to connect to a MySQL database. You'll need to learn this anyhow. Then you can set it up so your dad can upload what he buys and the prices in an excel spreadsheet and have your program automatically estimate the price based on what he's already paid for things in the past.


FB | Blog
If you ever wondered who to blame for your problems, find a mirror.

BBS Signature
PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Artist
Response to Modularization? Pssshhh (java) 2012-09-02 10:00:30 Reply

At 8/21/12 02:18 AM, kaiser-d wrote:
srsly why use Java?
Because Java gets you work. Its in higher demand than pretty much anything right now, and seeing as though it's the basis for android app development, it's probably going to stay that way for the forseeable future. Good on you.

Java is an overused language that is already out of date. It use to be the big language in the 80s but we have improved from it. people use it in web development alot, because alot of personal computers havn't been pushed away from the java frameworks that were created and its easier to communicate. The reason its so widely used is because Oracle pushes the language so much and has influenced the educational system with it.

In reality is an out of date language that isn't really needed.

Also netbeans isn't that great either. You want to learn GUI without netbeans at first so i would need something like eclipse. If i use drag and drop box to do work in netbeans im learning nothing. Just like if i use eclipse to correct my errors. In blueJ it doesn't do anything but abstract visual things and make it easier for me to comprehend whats going on. So BlueJ is a educational program and is very good at what it does. You can take what you write from blueJ and put it in eclipse, then put it in netbeans for the GUI drag and drop.

Atleast those are my plans. WHy develop in just one IDE when they all have their strengths and weaknesses.

Vedvin
Vedvin
  • Member since: Oct. 5, 2009
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Modularization? Pssshhh (java) 2012-09-02 13:06:02 Reply

At 9/2/12 10:00 AM, PMMurphy wrote:
At 8/21/12 02:18 AM, kaiser-d wrote:
srsly why use Java?
Because Java gets you work. Its in higher demand than pretty much anything right now, and seeing as though it's the basis for android app development, it's probably going to stay that way for the forseeable future. Good on you.
Java is an overused language that is already out of date. It use to be the big language in the 80s but we have improved from it. people use it in web development alot, because alot of personal computers havn't been pushed away from the java frameworks that were created and its easier to communicate. The reason its so widely used is because Oracle pushes the language so much and has influenced the educational system with it.

In reality is an out of date language that isn't really needed.

Also netbeans isn't that great either. You want to learn GUI without netbeans at first so i would need something like eclipse. If i use drag and drop box to do work in netbeans im learning nothing. Just like if i use eclipse to correct my errors. In blueJ it doesn't do anything but abstract visual things and make it easier for me to comprehend whats going on. So BlueJ is a educational program and is very good at what it does. You can take what you write from blueJ and put it in eclipse, then put it in netbeans for the GUI drag and drop.

Atleast those are my plans. WHy develop in just one IDE when they all have their strengths and weaknesses.

Java didn't even exist in the 80's. It appeared in 1991, but wasn't really available until 1995, and didn't gain much popularity before v2 in 1999. It is still growing rapidly in popularity but not really on the web, but in enterprise environments and embedded systems (such as cars and home electronics). Many large corporations such as Ericsson have ditched their old C servers recently in favor of Java Enterprise since it is much cheaper to maintain and develop for.