1,075,809 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by cafegeo

So I finally firgured out how to call the method!!

    setColor (AnimalColor.ORANGE);
        getColor();

ALthough Im not sure if I even need a getColor in the Fox class. Since I am just setting the color is getColor() necessary?

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Well I have not compiled it but Im not getting any errors from eclipse which is good. Can you post the coding and slight explanation of calling these set and get methods from the Fox class? I am at a loss with calling them from the Fox class. Fox extends Animal.

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Makes sense it is not a string, it is of type AnimalColor. Does this look right?

public void setColor(AnimalColor c){
        this.c = myColor;
    }
    public AnimalColor getColor(){
        return myColor;
    }
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Is this the proper coding for set and get?

public void setColor(String c){
        this.c = myColor;
    }

    public String getColor(){
        return c();
    }
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

So then I will place them in the AnimalColor class right? And then access them through the Animal and Fox class?

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Since I changed the object private AnimalColor myColor; from protected to private, my other functions cannot access it. I get the error "The field Animal.myColor is not
visible
" I am supposed to use an API to access the AnimalColor enum function. Here are the exact instructions:

The current version of Animal makes myColor protected so that subclasses can access it. This is bad programming style.
Improve it by making myColor private
This will involve writing an API for myColor and retrotting it into the code, so that Animal and Fox use
the API

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Hello all, I am a beginner at Java and taking a class which is moving at a fast pace.
So I have this Animal class and Fox class that must access the AnimalColor class through an API. Originally the AnimalColor object in the Animal class was public(accessible by all) but I now must change that to Private and force the Fox and Animal class to access it through an API. I am new to Java and having trouble locating info on API's used in this format. Here is the code for Animal:

import java.util.Random;  
public abstract class Animal{

    /** 
     * myColor is protected so that subclasses can access them directly.
     * Not very good programming practice (but helps to illustrate private)
     * Better would be, like location, to have API methods to handle color.
     */
    private Point location;
    private AnimalColor myColor;    //Changed from public to private
    Animal

    /**
     * The constructor sets up the animal at a random location
     */
    Animal(){
        Random ranGen = new Random();
        location = new Point(10 + ranGen.nextInt(380), 10 + ranGen.nextInt(360));
    }

    /**
     * The API allows the animal's location to be altered. This uses the Point API.
     */
    public void setLocationX(int x){
        location.setX(x);
    }

    public void setLocationY(int y){
        location.setY(y);
    }

    public int getLocationX(){
        return location.getX();
    }

    public int getLocationY(){
        return location.getY();
    }

    public AnimalColor getColor(){
        return myColor;
    }

    /**
     * Abstract method
     */
    public abstract void move();
}

Here is the code for Fox:

public class Fox extends Animal{
    /**
     * All the constructor does is to set the color in which the fox should be
     * displayed.
     */
    Fox(){
        super();
        myColor = AnimalColor.ORANGE;
    }

    /**
     * Right now, foxes don't know how to move. This is not an abstract method because
     * it has a (null) body
     */
    public void move(){
        ;
    }

}

Finally the AnimalColor class which needs to be accessed by Fox and Animal through an API:

public enum AnimalColor {BLACK, WHITE, ORANGE};

I will be using this (API's) a great deal in this class and would love to be able to not only code it, but understand how to apply it. I know the reason for an API, but simply cannot apply it.

Thank you in advance for all your advice and help.

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I'm not exactly sure, but buttons[] is an array. Could I use that or should I be creating a new array?

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Hello everyone. I am starting a class that utilizes Java for software design. I have no previous java experience, although I have taken c++ prior to this and appreciate all the help I have received in the past. I also would like to thank everyone in advance for all your help and attention.

I have a program which randomly displays ellipses in a frame when a certain JButton is pressed. The paintComponent below colors in the ellipse. In this paintComponent I have pasted below there is a standard For loop. We have to change that to a For|In (int i : args) loop. I am not at all familiar with that kind of loop and am having trouble implementing it. I would appreciate any help or advice.

public void paintComponent(Graphics g){               
        Graphics2D g2 = (Graphics2D)g;                    
        Shape shapes[] = new Shape[count];
        g2.setPaint(Color.white);
        Random randomGenerator = new Random();

        for(int i=0, i < count, i++){                    *//For loop that needs to be changed to For|In*
            int newX = randomGenerator.nextInt(200);
            int newY = randomGenerator.nextInt(200);
            shapes[i] = new Ellipse2D.Float(50+newX, 50+newY, 100, 50);    
            g2.setPaint(Color.red);
            g2.fill(shapes[i]);
                     }
        }
    }

    // Set the value of count to reflect which button was pressed.

    public void actionPerformed(ActionEvent e){       
        if(e.getSource() == buttons[0]){
            count = 1;
        }
        if(e.getSource() == buttons[1]){
            count = 2;
        }
        if(e.getSource() == buttons[2]){
            count = 3;
        }
        if(e.getSource() == buttons[3]){
            count = 4;
        }
        if(e.getSource() == buttons[4]){
            count = 5;
        }
        repaint();
    }
}

Thank you in advance.

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I hope this is how it should be....Still there can be errors, after all its your code, it just formatted it as i think it is should be.....

Thank you very much for the edit. It is much better looking this way. I have posted on this forum before and my code never rubbed anyone the wrong way, but I guess all of these For, If and Else loops threw my organizational skills off.

I am hoping people will still take a look at my code and provide some advice.

The algorithm seems precise, but for some reason it is not working as it should.

Thank you again for your understanding patience.

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I'm not sure what you mean by "make more stuff up". What exactly am I making up?

I would appreciate the constructive criticism alone, without the senseless, arrogant insult.

I reposted my question below along with the edited code.

Hello all. This program is supposed to maintain inventory for 5 warehouses with 3 items each warehouse. Each in a different city. You can place an order or send a shipment to each warehouse. When you place an order, if one warehouse does not have enough of the item it searches through other warehouses to fulfill the order. If it finds one to fulfill the order, it takes from that warehouse and adds 10% to the price of the order. If it cannot find a warehouse to fulfill the order it simply says "Order Unfilled". I have the following code and rather than search through other warehouses, it simply says "Order Unfilled", even when other warehouse have enough product to fulfill. I would appreciate any advice, thank you in advance.

int quantityamount1[3];
		
for(int i=0;i<3;i++)
{
 cout<<"Enter quantity for item "<<i+1<<":\n";
 int quantity=0;
 cin>>quantity;
   if(quantity<=wh[0].amt[i] && wh[0].amt[i]>0)  //Searching current warehouse inventory
  
    wh[0].amt[i]-=quantity;
    quantityamount1[i]=quantity;
   }
    else
   {
     for(int j=1;j<5;j++)
     {
      if(wh[j].amt[i]>0 && quantity<=wh[j].amt[i])  //Searching other warehouses to fulfill order
      {
       cout<<quantity<<" quantity of item "<<i<<" shipped from "<<wh[j].city<<" to "<<wh[i].city<<"\n";
       wh[j].amt[i]-=quantity;
       cout<<wh[j].city<<"  "<<wh[j].amt[0]<<"  "<<wh[j].amt[1]<<"  "<<wh[j].amt[2]<<"\n";
       break;
      }
       else
       {
	cout<<"Order Unfilled\n";//Could not find any warehouses to fulfill order
	quantityamount1[i]=1;
	break;
       }
     }
	quantityamount1[i]=quantity;
   }
 }
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Thank you for the quick reply!

I removed the break and all that does is display "Order unfilled" 5 times consecutively. When removing the else block all together it doesn't seem to look at other warehouses at all.

I even started the "j" for loop at j=1, to skip the first warehouse,wh[0], but no change. It still says order unfilled and never searches the other warehouses.

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

30 minutes have passed so I could not edit my original. Sorry for the repost. Hope this is more readable.

int quantityamount1[3];
	
        for(int i=0;i<3;i++)
        {
         cout<<"Enter quantity for item "<<i+1<<":\n";
	 int quantity=0;
	 cin>>quantity;
           if(quantity<=wh[0].amt[i] && wh[0].amt[i]>0)  //Searching current warehouse inventory
           { 
	    wh[0].amt[i]-=quantity;
	    quantityamount1[i]=quantity;	     
           }
             else
              {
	        for(int j=0;j<5;j++)
                {
                 if(wh[j].amt[i]>0 && quantity<=wh[j].amt[i])  //Searching other warehouses to fulfill order
		 {
                   cout<<quantity<<" quantity of item "<<i<<" shipped from "<<wh[j].city<<" to "<<wh[i].city<<"\n";
	           wh[j].amt[i]-=quantity;
	           cout<<wh[j].city<<"  "<<wh[j].amt[0]<<"  "<<wh[j].amt[1]<<"  "<<wh[j].amt[2]<<"\n";
			break;
		  }
                    else
                    {
	             cout<<"Order Unfilled\n";//Could not find any warehouses to fulfill order
		     quantityamount1[i]=1;
		     break;
		    }
	         }
		  quantityamount1[i]=quantity;
             }
}
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Solved: Trouble with loops in C++

Hello all. This program is supposed to maintain inventory for 5 warehouses with 3 items each warehouse. Each in a different city. You can place an order or send a shipment to each warehouse. When you place an order, if one warehouse does not have enough of the item it searches through other warehouses to fulfill the order. If it finds one to fulfill the order, it takes from that warehouse and adds 10% to the price of the order. If it cannot find a warehouse to fulfill the order it simply says "Order Unfilled". I have the following code and rather than search through other warehouses, it simply says "Order Unfilled", even when other warehouse have enough product to fulfill. I would appreciate any advice, thank you in advance.

switch(ch){
case 1:
	int quantityamount1[3];
	
         for(int i=0;i<3;i++){
         cout<<"Enter quantity for item "<<i+1<<":\n";
	 int quantity=0;
	 cin>>quantity;
	   
           if(quantity<=wh[0].amt[i] && wh[0].amt[i]>0){ //Searching current warehouse inventory
	    wh[0].amt[i]-=quantity;
	      quantityamount1[i]=quantity;
         	     
                     }else{
			for(int j=0;j<5;j++){
                          if(wh[j].amt[i]>0 && quantity<=wh[j].amt[i]){//Searching other warehouses to fulfill order
			  
                          cout<<quantity<<" quantity of item "<<i<<" shipped from "<<wh[j].city<<" to "<<wh[i].city<<"\n";
			  wh[j].amt[i]-=quantity;
			  cout<<wh[j].city<<"  "<<wh[j].amt[0]<<"  "<<wh[j].amt[1]<<"  "<<wh[j].amt[2]<<"\n";
				break;
				  }else{
				    cout<<"Order Unfilled\n";//Could not find any warehouses to fulfill order
		         		 quantityamount1[i]=1;
					break;
					  }
				  }
			  quantityamount1[i]=quantity;
			  }
			}

Here is my warehouse data:

char ch;
	wh[0].city="New York";
	wh[0].amt[0]=40;
	wh[0].amt[1]=33;
	wh[0].amt[2]=43;
	
	wh[1].city="Los Angeles";
	wh[1].amt[0]=25;
	wh[1].amt[1]=23;
	wh[1].amt[2]=26;

	wh[2].city="Miami";
	wh[2].amt[0]=43;
	wh[2].amt[1]=23;
	wh[2].amt[2]=53;

	wh[3].city="Houston";
	wh[3].amt[0]=35;
	wh[3].amt[1]=35;
	wh[3].amt[2]=35;
	
	wh[4].city="Chicago";
	wh[4].amt[0]=21;
	wh[4].amt[1]=21;
	wh[4].amt[2]=21;
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Thank you very much. This is a great outline! I will get working. I built two structures so I think I'm half way there.

I will definitely take you up on the free code for 3 of those steps, not sure which ones just yet.

Thanks again!

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I'm not sure if my title makes sense, but I have an assignment and need some guidance on how to approach this.

It's an Accounts Receivable program for ABC Hardware Company. I have a Master file with cust numbers, cust name and balance due. I also have a Transaction file with cust number, transaction type(Payment or Order), item ordered, quantity and total amount. If the transaction is a Payment I subtract from the customer balance due. If its an order I add it. There should be more than one transaction record for each master record.

I am having a tough time figuring out how to first approach this without confusing myself. I have a structure and created the two files but I'm not sure in which order I should do things. I appreciate any advice you can give that will put me in the right direction.

Thank you in advance.

My output should have:

[B]Customer Name    Customer Number
                 Previous Balance $


Transaction#  Item Ordered  Order Amount $
Transaction#  Payment       Payment Amount $

                            Balance Due $
[/B]
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I was supposed to include "payment" in the item field according to the assignment, that will make this easier. No need for If statement since I will be printing out all fields regardless.

while (!datafile.eof())       
   {            
    for (int i = 0; i < num_trans; i++)
    {
          datafile >> cust[i].type >> cust[i].custnum >> cust[i].item >> cust[i].qty >> cust[i].amnt;                     
          cout << cust[i].type << "  " << cust[i].custnum << "  " << cust[i].item << "  " << cust[i].qty << "  " << cust[i].amnt << "  \n";    
    }
cin.get();    
   return 0;
   }
}
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

Hello everyone. I am trying to pull data from a file and show it on the screen. There is a condition in the file that says if it is a "payment" or "order". A "P" and an "O" in the first column. My output becomes an infinite loop. I have an if statement because if it's a payment, there are less fields in the file and the datafile statement might confuse the "item" field when it is an order, for the "amnt" field when it's a payment.

Ultimately this is for an accounts receivable program that uses this file to manipulate a customer balance file. For now I want to just display the file contents.

Thank you in advance:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct record  //Transaction.
{
       char name[14];
       char type[1];
       int custnum;
       int transnum;
       char item[6];
       int qty;
       int amnt;
       int balance;
};

int main()
{
const int num_cust = 7; //num of customers
record cust[num_cust]; 


cout << "ABC Hardware Company \n\n";  

 fstream datafile;                                   
    datafile.open("trans.txt");
    if(!datafile)
    {
      cout << "Could not open file. \n\n";          
      cin.get();
      cin.get();
    
       return 0;
    }           
   while (!datafile.eof())       
   {            
    for (int i = 0; i < num_cust; i++)    
        {             
          if ('cust[i].type == O')
          {                                        
           datafile >> cust[i].type >> cust[i].custnum >> cust[i].item >> cust[i].qty >> cust[i].amnt; 
           cout << cust[i].type << "  " << cust[i].custnum << "  " << cust[i].item << "  " << cust[i].qty << "  " << cust[i].amnt "  ";
           
          } 
           else 
            datafile >> cust[i].type >> cust[i].custnum >> cust[i].amnt;
            cout << cust[i].type << "  " << cust[i].custnum << "  ";
          
        }    
    }
cin.get();    
   return 0;
}

DATA FILE:

O 1111 BOLTS 5 1
O 1111 SCREWS 6 5
P 1111 20
P 1111 5
O 1111 NAILS 3 2
O 2222 NAILS 1 2
P 2222 1
O 2222 HAMMER 1 10
O 2222 DRIVER 2 5
P 2222 10
O 3333 LADDER 1 10
P 3333 5
P 3333 1
O 3333 BELT 1 5
O 3333 GLOVES 1 3
O 4444 BOLTS 5 1
O 4444 SCREWS 6 5
P 4444 20
P 4444 5
O 4444 NAILS 3 2
O 5555 NAILS 1 2
P 5555 1
O 5555 HAMMER 1 10
O 5555 DRIVER 2 5
P 5555 10
O 6666 LADDER 1 10
P 6666 5
P 6666 1
O 6666 BELT 1 5
O 6666 GLOVES 1 3
O 7777 DRILL 1 20
O 7777 SCREWS 2 5
O 7777 BATTERY 2 10
P 7777 30
P 7777 50

cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

I got it!! Makes perfect sense.

Compare one thing, worker.net, then swap the entire struct, worker. Keeps it all together. now it works no matter where it is positioned when I call it in Main().

Thank you!


void net_paid (person worker[], const int emps)  //7.Takes in age, base pay and amnt of workerloyees.
{                                                        
 int index=0;
 person temp;   //Completely forgot to set temp to the struct.
 
        
      for(int i=0;i<emps;i++)
      {
       for(int j=i+1;j<emps;j++)
       {
        if(worker[i].net < worker[j].net)  //Compare one thing.
        {
         temp = worker[i];                 //Swap entire struct.
         worker[i] = worker[j];
         worker[j] = temp;               
        }    
       }
     
      }
}
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0

The swap variable has me a little confused, but I removed also from my "name sort" function and it still sorts properly.

I am at a loss with this one. From my inexperienced eyes both sorts look right. They work perfectly on their own. Below is the other sort which sorts the names. As soon as I move the calling of this sort to the first one in Main(), it sorts .lname in alphabetical order, for the rest of the program. If I move it to the bottom of the list of called functions, everything works fine and it only sorts whats in that function.

void name_sort(person worker[], const int emps) //Alphabetizes first and last name.
{
  char temp[8];
  char tempf[8];

  cout << "\n";
  cout << "Miser Corporation employees in alphabetical order: \n\n";
   
     for (int i=0;i<emps;i++)
     {
         for (int j=i+1;j<emps;j++)
       {
         if (strcmp(worker[i].lname,worker[j].lname)>0) //Sorting Last Name of employee
         {
              strcpy(temp,worker[i].lname);
              strcpy(worker[i].lname,worker[j].lname);
              strcpy(worker[j].lname,temp); 
         }
         if (strcmp(worker[i].fname,worker[j].fname)>0) //Matching First name with sorted last name
         {
              strcpy(tempf,worker[i].fname);
              strcpy(worker[i].fname,worker[j].fname);
              strcpy(worker[j].fname,tempf);                                           
         }
       }
      cout << worker[i].lname << " " << worker[i].fname << "\n"; //Printing out first and last name sorted by last
     }
}
cafegeo
Light Poster
49 posts since Apr 2011
Reputation Points: 20
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.1258 seconds using 2.63MB