Question on compostion in java

Discussion in 'Mac Programming' started by Hylekmi, Jun 6, 2012.

  1. macrumors regular

    Joined:
    Dec 8, 2010
    #1
    Hey guys I am writing this program for my final where i have to create a library database type thing and then create a GUI for it. I have 3 fields(author, title, and due date) i have the author and title but i cant figure out how to pass the due date to the other object. Here is my classes...

    Code:
      
    
    
    public class Date
    {
        private int month;    //variable to store the month
        private int day;      //variable to store the day
        private int year;     //variable to store the year
    
       //sets all three fields to -1;
        public Date()
        {
            setMonth(-1);
            setDay(-1);
            setYear(-1);
        }
    
    	//sets the values by invoking the setDate method
        public Date(int month, int day, int year)
        {
    		setDate(month, day, year);
        }
    
        public void setDate(int month, int day, int year)
        {
            setMonth(month);
            setDay(day);
            setYear(year);
        }
    
    	public void setDay(int d) {  day = d; }
    	public void setMonth(int m) {  month = m; }
    	public void setYear(int y) {  year = y; }
    
       public int getMonth()	{ return month; }
       public int getDay()     	{ return day; }
       public int getYear()		{ return year; }
    
        //Returns the date in the form mm-dd-yyyy
        public String toString()
        {
            return (month + "-" + day + "-" + year);
        }}
    
    Code:
     
    public class testFinalProjectEver 
    {
    
    	public static void main(String[] args)
    	{
    		libraryBook b1 = new libraryBook();//instantiantion
    		Date d1= new Date();
    		
    		d1.setDate(02, 28, 1991);
    		b1.setAuthor("Mike");
    		b1.setTitle("Poop");
    		System.out.println(b1.toString());
    	}
    }
    Code:
      import java.util.Date;
    
    
    public class libraryBook
    {
    	private String title;
    	private String author;
    	private Date dueDate;
    	
    	public libraryBook()
    	{
    		title="XXXBLANKXXXXX";
    		author="XXXXBLANKXXXX";		
    	}//end default constructor
    	
    	public libraryBook(String theTitle,String theAuthor,Date theDueDate)
    	{
    		author=theAuthor;
    		title=theTitle;
    		dueDate=theDueDate;
    	}
    	//DEFAULT CONSTRUCTOR
    
    
    	public void setTitle(String ti)		{title=ti;}
    	public void setAuthor(String au)	{author=au;}
    	public void setDate(Date da)		{dueDate=da;}
    	
    	public String getTitle()	{return title;}
    	public String getAuthor()	{return author;}
    	public Date getDueDate()	{return dueDate;}
    	
     
    	
    	public String toString()
    	{
    		String s;
    		return s=("The title is "+getTitle()+", The author is"+ getAuthor()+",The Due Date is "+getDueDate());
    	
    	}
    
    	
    		
    	
    
    
    	
    
    	
    	
    	
    }//end libraryBook
    
    please help
     
  2. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #2
    In your libraryBook class, which Date class are you referencing? Which Date class should you be referencing?
     
  3. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #3
    I dont know, I am using eclipse and it keeps prompting me to use the date class in java. the teacher said that i need to use his date class...so i have no clue how to do it
     
  4. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #4
    Anyone? I am in real big trouble here and its due at midnight central time.
     
  5. macrumors 65816

    Joined:
    Mar 13, 2012
    #5
    I don't know Java, but looking at your code, I would guess you have a namespace conflict. Either rename your own Date class to something else, or drop the import of the Java Date namespace (i.e. lose this line: import java.util.Date; ).

    Just a guess.
     
  6. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #6
    Thankyou that helped a little. I still cant get it to accept the date object in the librarybook class
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    You shouldn't import any other Date object. Are you compiling Date.java in the same directory as your main class? . should be in your classpath. I'm afraid using eclipse at this stage is really hindering you. Save all of your files, open terminal, cd to the directory your source is in, and run "javac *.java". Then run "java testFinalProjectEver". Delete the import of java's Date, try the above, see how it goes.

    -Lee
     
  8. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #8
    I took Date.java out and now am just using my own code with no imports.I dont know how to set the date field in my LibraryBook Class to where it will hold the object in the date class.
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    Pass d1 (baaaad variable name) to b1's (also very bad) setDate method that accepts a Date.

    -Lee
     
  10. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #10
    Thats what i have been trying to do...i just dont know how.
     
  11. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    In main:
    b1.setDate(d1);

    -Lee
     
  12. macrumors 6502a

    Joined:
    Feb 18, 2008
    #12
    That Date class should be called something else. Giving it the same name as an API class is asking for trouble.
     
  13. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #13
    Heres another question... I made a GUI and i am trying to set the value of date(year) to whatever the use inputs into the textfield. I need to convert from a String to an Int. Im trying to use parseInt and its saying that "The method parseInt(String) is undefined for the type testFinalProjectEver.addBookHandler"

    I dont get this becuase the whole point of ParseInt is to accept a string. heres my code.
    Code:
      import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    import javax.swing.*;
    
    
    public class testFinalProjectEver 
    {
    
    	
    	public static void main(String[] args)
    	{	new testFinalProjectEver();
    		libraryBook b1 = new libraryBook();//instantiantion
    		date d1= new date();
    		
    		//d1.setDate(02, 28, 1991);
    		b1.setAuthor("Mike");
    		d1.setDate(01,8,1999);
    		b1.setTitle("Poop");
    		b1.setDate(d1);
    		
    		System.out.println("Title: "+b1.getTitle()+"\n"+"Author:"+b1.getAuthor()+"\n"+d1.getDay());
    	
    	}
    		
    private JFrame frame;
    private JPanel panelBar,panelMain;
    private JButton buttonDone,buttonAdd,abHandler,addBookHandler,
    doHandler,doneHandler;
    ;
    private JLabel titleL,labelTwo,authorL,dateDL,dateYL,dateML;
    static JTextField titleTF;
    static JTextField authorTF;
    static JTextField dueDateYTF;
    static JTextField dueDateMTF;
    static JTextField dueDateDTF;
    
    public testFinalProjectEver()
    {
    	 GUI();
    }
    
    public void GUI()
    {
    	frame=new JFrame("Final Project");
    	frame.setVisible(true);
    	frame.setSize(900,600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	panelBar= new JPanel();
    	panelMain=new JPanel();
    	panelBar.setBackground(Color.YELLOW);
    	
    	titleL = new JLabel("Enter the title:");
    	authorL= new JLabel("Enter the Author:");
    	dateDL=  new JLabel("Enter the Day Field:");
    	dateYL=  new JLabel("Enter the Year Field:");
    	dateML=  new JLabel("Enter the Month Field:");
    	buttonDone=new JButton("Done");
    	buttonAdd=new JButton("Add A Book");
    	
    	titleTF=new JTextField();
    	authorTF=new JTextField();
    	dueDateYTF=new JTextField();
    	dueDateMTF=new JTextField();
    	dueDateDTF=new JTextField();
    	
    	buttonDone.addActionListener(new ActionListener(){
    		
    		public void actionPerformed(ActionEvent e){
    			
    			JOptionPane.showMessageDialog(null, "Please Enter a file name:");
    		}
    	});
    	//instantiate listnerers
    	abHandler=new addBookHandler();
    	doHandler=new doneHandler();
    	
    	//register the listners with there respective components
    	buttonAdd.addActionListener(abHandler);
    	buttonDone.addActionListener(doHandler);
    	
    	
    	panelBar.add(buttonDone);
    	panelBar.add(buttonAdd);
    	panelMain.add(titleTF);
    	panelMain.add(authorTF);
    	panelMain.add(dueDateYTF);
    	panelMain.add(dateYL);
    	panelMain.add(dueDateMTF);
    	panelMain.add(dueDateDTF);
    	panelMain.add(titleL);
    	panelMain.add(authorL);
    	panelMain.add(dateML);
    
    	frame.setLayout(new GridLayout(6,2));
    	
    	frame.add(titleL);
    	frame.add(titleTF);
    	frame.add(authorL);
    	frame.add(authorTF);
    	frame.add(dateYL);
    	frame.add(dueDateYTF);
    	frame.add(dateDL);
    	frame.add(dueDateDTF);
    	frame.add(dateML);
    	frame.add(dueDateMTF);
    	frame.add(panelBar);
    
    }//end go
    	private class addBookHandler implements ActionListener {
    
    
    		public void actionPerformed(ActionEvent e) {
    			
    			String value=" ";
    			 
    			String authorName = testFinalProjectEver.authorTF.getText() ;
    			String bookTitle = testFinalProjectEver.titleTF.getText() ;
    			value=testFinalProjectEver.dueDateYTF.getText();
    			
    				date year= setYear(parseInt(value));
    			
    			
    			
    			
    			libraryBook b1 = new libraryBook();//instantiantion
    
    				b1.setAuthor(authorName);
    				b1.setTitle(bookTitle);
    				b1.setDate(year);
    				
    			
    			
    		}
    
    	}
    
    
    
     
  14. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #14
    It's because you don't have a parseInt method in your class.

    JavaDocs! JavaDocs! JavaDocs! JavaDocs! JavaDocs! JavaDocs! JavaDocs!

    If I were teaching Java that's what I would emphasize - you must know how to read the API docs.

    http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

    By the way, Java conventions specify that class names should start with an upper case letter. No, it's not enforced in the compiler but it makes your code harder for others to read if they have to check if you're using a Class name or an instance variable.

    Rename testFinalProjectEver to TestFinalProjectEver, libraryBook to LibraryBook
     
  15. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #15
  16. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #16
    So you don't know how to access static methods in other classes?

    Google for "how to parse int java"
     
  17. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #17
    Its not static though...its an accessor...I think im missing something exteremely obvious.
     
  18. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #18
    You need to parse a String into an int. I've given you the link to the JavaDocs where the most commonly used parseInt method is described. I've also suggested a Google search.

    If you can't understand why you get a "The method parseInt(String) is undefined ..." error for the code you've written, you have failed to understand something very fundamental to coding, and you probably deserve to fail your course, or your teacher deserves to be criticized for not teaching you.
     
  19. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #19
    I realize i need to parse a string into an Int. I know what undefined means. I cant get set date to accept the user input from the GUI . Im asking for help to understand this(ie EXPLAIN) not to be criticized. Im not a programmer, nor will i ever be a programmer. This will be the last program i ever design. I just need to finish it.
     
  20. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #20
    Parsing a String into an int isn't that hard.
    Code:
    Integer.parseInt(String)
    ... is what you need. Note that this can throw a NumberFormatException if the input string cannot be parsed.
     
  21. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #21
    Code:
    date year= setYear(parseInt(value));
    
    libraryBook b1 = new libraryBook();//instantiantion
    
    b1.setAuthor(authorName);
    b1.setTitle(bookTitle);
    b1.setDate(year);
    
    Once you figure out the parseInt problem, this bit of code is going to give you problems.

    1. The method setYear does not return anything so your variable "year" is going to be null
    2. The method setDate requires 3 parameters not just the 1 "year" that you are sending it.
     
  22. thread starter macrumors regular

    Joined:
    Dec 8, 2010
    #22
    Sorry, i got it about 30 minutes ago. I already changed all of that. Thank you for your help
     

Share This Page