PDA

View Full Version : Question on compostion in java




Hylekmi
Jun 6, 2012, 07:38 PM
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...




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);
}}



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());
}
}

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



naples98
Jun 6, 2012, 08:15 PM
In your libraryBook class, which Date class are you referencing? Which Date class should you be referencing?

Hylekmi
Jun 6, 2012, 08:19 PM
In your libraryBook class, which Date class are you referencing? Which Date class should you be referencing?

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

Hylekmi
Jun 6, 2012, 09:29 PM
Anyone? I am in real big trouble here and its due at midnight central time.

bogatyr
Jun 6, 2012, 09:31 PM
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.

Hylekmi
Jun 6, 2012, 09:38 PM
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.

Thankyou that helped a little. I still cant get it to accept the date object in the librarybook class

lee1210
Jun 6, 2012, 09:47 PM
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

Hylekmi
Jun 6, 2012, 09:49 PM
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

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.

lee1210
Jun 6, 2012, 10:57 PM
Pass d1 (baaaad variable name) to b1's (also very bad) setDate method that accepts a Date.

-Lee

Hylekmi
Jun 6, 2012, 11:04 PM
Pass d1 (baaaad variable name) to b1's (also very bad) setDate method that accepts a Date.

-Lee

Thats what i have been trying to do...i just dont know how.

lee1210
Jun 6, 2012, 11:44 PM
In main:
b1.setDate(d1);

-Lee

knightlie
Jun 7, 2012, 09:23 AM
That Date class should be called something else. Giving it the same name as an API class is asking for trouble.

Hylekmi
Jun 7, 2012, 11:51 AM
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. 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);



}

}

plinden
Jun 7, 2012, 11:56 AM
"The method parseInt(String) is undefined for the type testFinalProjectEver.addBookHandler"

date year= setYear(parseInt(value));


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

Hylekmi
Jun 7, 2012, 12:00 PM
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)

So then how would I do this then?

plinden
Jun 7, 2012, 12:12 PM
So then how would I do this then?

So you don't know how to access static methods in other classes?

Google for "how to parse int java"

Hylekmi
Jun 7, 2012, 12:15 PM
So you don't know how to access static methods in other classes?

Google for "how to parse int java"

Its not static though...its an accessor...I think im missing something exteremely obvious.

plinden
Jun 7, 2012, 12:37 PM
Its not static though...its an accessor...I think im missing something exteremely obvious.

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.

Hylekmi
Jun 7, 2012, 12:46 PM
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.

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.

wrldwzrd89
Jun 7, 2012, 01:34 PM
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.
Parsing a String into an int isn't that hard.
Integer.parseInt(String)
... is what you need. Note that this can throw a NumberFormatException if the input string cannot be parsed.

naples98
Jun 7, 2012, 01:48 PM
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.

Hylekmi
Jun 7, 2012, 02:13 PM
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.

Sorry, i got it about 30 minutes ago. I already changed all of that. Thank you for your help