Class Book

set and get Methods

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2883 Views - Last Post: 27 April 2010 - 06:59 PM Rate Topic: -----

#1 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Class Book

Posted 19 April 2010 - 09:15 PM

am having problems implement methods get and set? for java? this is my task but i'm a little confused!!! the circled programs on the jpg file is what the instruc wants.. also, i here is my code in the zip. Thank you, if you can help!!!

Attached image(s)

  • Attached Image

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: Class Book

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 19 April 2010 - 09:39 PM

Please post your code into the body of your post, using code tags like so: :code:

Also, can you more specifically describe the errors or problems you are encountering? Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#3 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 20 April 2010 - 02:17 AM

View Postmacosxnerd101, on 19 April 2010 - 07:39 PM, said:

Please post your code into the body of your post, using code tags like so: :code:

Also, can you more specifically describe the errors or problems you are encountering? Thanks for helping us help you! :)




The code is attached in the zip file. i included the methods in one and in the other i am calling the methods from the main file.
Was This Post Helpful? 0
  • +
  • -

#4 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 20 April 2010 - 02:23 AM

View Post_marlon, on 20 April 2010 - 12:17 AM, said:

View Postmacosxnerd101, on 19 April 2010 - 07:39 PM, said:

Please post your code into the body of your post, using code tags like so: :code:

Also, can you more specifically describe the errors or problems you are encountering? Thanks for helping us help you! :)




The code is attached in the zip file. i included the methods in one and in the other i am calling the methods from the main file.




Here is the code :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;

public class book{

Scanner myScanner = new Scanner(System.in);
String getTitle;
String getpublisher;
String getISBN;
String getPrice;
String getCopyRight;
String getAuthor1;
String getAuthor2;
String getAuthor3;
String getAuthor4;
int getAuthorCount;


void getTitle(){

getTitle = JOptionPane.showInputDialog("Enter title:");
JOptionPane.showMessageDialog(null,getTitle, "This is your book bitle: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getpublisher(){

getpublisher = JOptionPane.showInputDialog("Enter publisher:");
JOptionPane.showMessageDialog(null,getpublisher, "This is your publisher: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getISBN(){

getISBN = JOptionPane.showInputDialog("Enter ISBN:");
JOptionPane.showMessageDialog(null,getISBN, "This is your ISBN: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getPrice(){

getPrice = JOptionPane.showInputDialog("Enter price:");
JOptionPane.showMessageDialog(null,getPrice, "This is your price: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getCopyRight(){

getCopyRight = JOptionPane.showInputDialog("Enter copy right:");
JOptionPane.showMessageDialog(null,getCopyRight, "This is your copy right: ",
JOptionPane.INFORMATION_MESSAGE);
}
//method to get author count

void getAuthorCount(){

while(getAuthor1!=null)
getAuthorCount++;
if (getAuthor2!=null)
getAuthorCount++;
else if(getAuthor3!=null)
getAuthorCount++;
else if(getAuthor4!=null)
getAuthorCount++;

JOptionPane.showMessageDialog(null,getAuthorCount, "This is author count: ",
JOptionPane.INFORMATION_MESSAGE);

}

void getAuthor1(){

getAuthor1 = JOptionPane.showInputDialog("Enter author 1:");
JOptionPane.showMessageDialog(null,getAuthor1, "This is your book title: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getAuthor2(){

getAuthor2 = JOptionPane.showInputDialog("Enter author 2:");
JOptionPane.showMessageDialog(null,getAuthor2, "This is your author 2: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getAuthor3(){

getAuthor3 = JOptionPane.showInputDialog("Enter author 3:");
JOptionPane.showMessageDialog(null,getAuthor3, "This is your author 3: ",
JOptionPane.INFORMATION_MESSAGE);
}

void getAuthor4(){

getAuthor4 = JOptionPane.showInputDialog("Enter author 4:");
JOptionPane.showMessageDialog(null,getAuthor4, "This is your author 4: ",
JOptionPane.INFORMATION_MESSAGE);
}




// void getauthorCount(){
// do while((getAuthor1!=null)&&(getAuthor1!=null)&&(getAuthor1!=null)&&(getAuthor1!=null)&&)
// }
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


class GetInfo{

public static void main(String args[]){

book newGetName = new book();
newGetName.getTitle();

book newGetPublisher = new book();
newGetPublisher.getpublisher();

book newGetISBN = new book();
newGetISBN.getISBN();

book newGetPrice = new book();
newGetPrice.getPrice();

book newGetCopyRight = new book();
newGetCopyRight.getCopyRight();

book newGetAuthor1 = new book();
newGetAuthor1.getAuthor1();

book newGetAuthor2 = new book();
newGetAuthor2.getAuthor2();

book newGetAuthor3 = new book();
newGetAuthor3.getAuthor3();

book newGetAuthor4 = new book();
newGetAuthor4.getAuthor4();

book newGetAuthorCount = new book();
newGetAuthorCount.getAuthorCount();
}
}


So the problem is not necessarily it running properly, but making sure i am using the methods the right way.. for example, i don't know how to exactly implement the get or set methods and did this based on what i understood from my boook and from online videos on youtube. let me know my mistakes and what the heck to do.. once i implement the methods for my var(s) i have to call them using the main function with a GUI using java. Any help would be appreciated. Been working on this sh** for a while.. as you can see it's 2:20 and am still working on it!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 20 April 2010 - 04:14 AM

View Postmacosxnerd101, on 20 April 2010 - 12:39 AM, said:

using code tags like so: :code:


It seems like you missed the part about using code tags. Please remove the color tags, and add in code tags so we can more easily read your code.
Was This Post Helpful? 0
  • +
  • -

#6 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 20 April 2010 - 07:53 AM

View Postmacosxnerd101, on 20 April 2010 - 02:14 AM, said:

View Postmacosxnerd101, on 20 April 2010 - 12:39 AM, said:

using code tags like so: :code:


It seems like you missed the part about using code tags. Please remove the color tags, and add in code tags so we can more easily read your code.


Okay, so 11 hours later at 7am, this is what i have:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class book
{
static Scanner console = new Scanner(System.in);
private String title;
private String publisher;
private String isbn;
private String copyRight;
private String author1;
private String author2;
private String author3;
private String author4;
private int NumOfCopies;
private double price;
private int authorCount;

//contstructor
void book(String constTitle,
String constPublisher, String constIsbn,String constCopyRight,
String constAuthor1 , String constAuthor2,String constAuthor3,
String constAuthor4, int constNumOfCopies,double constPrice,int constAuthorCount)
{

title = constTitle;
publisher = constPublisher;
isbn = constIsbn;
copyRight = constCopyRight;
author1 = constAuthor1;
author2 = constAuthor2;
author3 = constAuthor3;
author4 = constAuthor4;
NumOfCopies = constNumOfCopies;
price = constPrice;
authorCount = constAuthorCount;
}


// Start of Set Method
void setTitle(String constTitle)
{
title = constTitle;
}

void setPublisher(String constPublisher)
{
publisher = constPublisher;
}

void setIsbn(String constIsbn)
{
isbn = constIsbn;
}

void setCopyRight(String constCopyRight)
{
copyRight = constCopyRight;
}

void setAuthor1(String constAuthor1)
{
author1 = constAuthor1;
}

void setAuthor2(String constAuthor2)
{
author2 = constAuthor2;
}

void setAuthor3(String constAuthor3)
{
author3 = constAuthor3;
}

void setAuthor4(String constAuthor4)
{
author4 = constAuthor4;
}

void setNumOfCopies(int constNumOfCopies)
{
NumOfCopies = constNumOfCopies;
}

void setPrice(float constPrice)
{
price = constPrice;
}

void setAuthorCount(int constAuthorCount)
{
authorCount = constAuthorCount;
}

// End of Set Method


// Start of Get Methods
public String getTitle()
{
title = JOptionPane.showInputDialog("Enter title:");
return title;
}

String getPublisher()
{
publisher = JOptionPane.showInputDialog("Enter publisher:");
return publisher;
}

String getIsbn()
{
isbn = JOptionPane.showInputDialog("Enter ISBN:");
return isbn;
}

String getCopyRight()
{
copyRight = JOptionPane.showInputDialog("Enter copy right:");
return copyRight;
}

String getAuthor1()
{
author1 = JOptionPane.showInputDialog("Enter author 1:");
return author1;
}

String getAuthor2()
{
author2 = JOptionPane.showInputDialog("Enter author 2:");
return author2;
}

String getAuthor3()
{
author3 = JOptionPane.showInputDialog("Enter author 3:");
return author3;
}

String getAuthor4()
{
author4 = JOptionPane.showInputDialog("Enter author 4:");
return author4;
}

double getPrice()
{
price = Float.parseFloat(JOptionPane.showInputDialog("Enter price:"));
return price;
}

int getNumOfCopies()
{
NumOfCopies = Integer.parseInt(JOptionPane.showInputDialog("Enter price:"));
return NumOfCopies;
}

// End of get Methods


//Start of authorCount Method

int getAuthorCount()
{
while (author1!=null)
{
authorCount++;
}
if (author2!=null)
{
authorCount++;
}
else if (author3!=null)
{
authorCount++;
}
else if (author4!=null)
{
authorCount++;
}


return authorCount;
}
//End of authorCount Method


//EOF
}







import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class bookTest
{
public static void main(String[]args)
{
String temp;

book getTitleObject = new book();

book getPublisherObject = new book();

book getIsbnObject = new book();

book getCopyRightObject = new book();

book getAuthor1Object = new book();

book getAuthor2Object = new book();

book getAuthor3Object = new book();

book getAuthor4Object = new book();

book getPriceObject = new book();
book getAuthorCountObject = new book();
book getNumOfCopiesObject = new book();

temp =
"This is your Title: "+getTitleObject.getTitle()+
"\n Publisher: "+getPublisherObject.getPublisher()+
"\n ISBN: "+getIsbnObject.getIsbn()+
"\n Copy Right: "+getCopyRightObject.getCopyRight()+
"\n Author1: "+getAuthor1Object.getAuthor1()+
"\n Author2: "+getAuthor2Object.getAuthor2()+
"\n Author3: "+getAuthor3Object.getAuthor3()+
"\n Author4: "+getAuthor2Object.getAuthor4()+
"\n Price: "+String.format("%.2f", getPriceObject.getPrice())+
"\n Num of Authors: "+getAuthorCountObject.getAuthorCount()+
"\n Num of Copies: " +getNumOfCopiesObject.getNumOfCopies();

JOptionPane.showMessageDialog(null,temp, " ",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);



}
}




so now my only issues are: getting the damn getAuthorCount to work properly because it's not, for some reason...thought not sure why (i've tried many ways including boolean and it don't work). Also, the next things would be to create a search function and to make sure that it updates it self and to create an array of book [100}. If anyone can help, it'd be nice. Thank you guys.

View Postmacosxnerd101, on 20 April 2010 - 02:14 AM, said:

View Postmacosxnerd101, on 20 April 2010 - 12:39 AM, said:

using code tags like so: :code:


It seems like you missed the part about using code tags. Please remove the color tags, and add in code tags so we can more easily read your code.

Okay, so 11 hours later at 7am, this is what i have:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class book
{
static Scanner console = new Scanner(System.in);	
private	String title; 
private	String publisher; 
private	String isbn;
private	String copyRight; 
private	String author1; 
private	String author2; 
private	String author3; 
private	String author4;
private int NumOfCopies;
private	double price;
private	int authorCount; 

//contstructor 
void book(String constTitle, 
String constPublisher, String constIsbn,String constCopyRight,
String constAuthor1 , String constAuthor2,String constAuthor3, 
String constAuthor4, int constNumOfCopies,double constPrice,int constAuthorCount) 
{

title = constTitle;
publisher = constPublisher; 
isbn = constIsbn;
copyRight = constCopyRight;
author1 = constAuthor1; 
author2 = constAuthor2; 
author3 = constAuthor3;
author4 = constAuthor4;
NumOfCopies = constNumOfCopies; 
price = constPrice;
authorCount = constAuthorCount;
}


// Start of Set Method 
void setTitle(String constTitle)
{
title = constTitle;
} 

void setPublisher(String constPublisher)
{
publisher = constPublisher;
} 

void setIsbn(String constIsbn)
{
isbn = constIsbn;
} 

void setCopyRight(String constCopyRight)
{
copyRight = constCopyRight;
} 

void setAuthor1(String constAuthor1)
{
author1 = constAuthor1;
} 

void setAuthor2(String constAuthor2)
{
author2 = constAuthor2;
} 

void setAuthor3(String constAuthor3)
{
author3 = constAuthor3;
}

void setAuthor4(String constAuthor4)
{
author4 = constAuthor4;
}

void setNumOfCopies(int constNumOfCopies)
{
NumOfCopies = constNumOfCopies;
}	

void setPrice(float constPrice)
{
price = constPrice;
}	

void setAuthorCount(int constAuthorCount)
{
authorCount = constAuthorCount;
}

// End of Set Method 


// Start of Get Methods 
public String getTitle()
{
title = JOptionPane.showInputDialog("Enter title:");	
return title;
}

String getPublisher()
{
publisher = JOptionPane.showInputDialog("Enter publisher:"); 
return publisher;
}	

String getIsbn()
{
isbn = JOptionPane.showInputDialog("Enter ISBN:"); 
return isbn;
}

String getCopyRight()
{
copyRight = JOptionPane.showInputDialog("Enter copy right:"); 
return copyRight;
}

String getAuthor1()
{
author1 = JOptionPane.showInputDialog("Enter author 1:"); 
return author1;
}

String getAuthor2()
{
author2 = JOptionPane.showInputDialog("Enter author 2:"); 
return author2;
}

String getAuthor3()
{
author3 = JOptionPane.showInputDialog("Enter author 3:");
return author3;
}

String getAuthor4()
{
author4 = JOptionPane.showInputDialog("Enter author 4:");
return author4;
}

double getPrice()
{
price = Float.parseFloat(JOptionPane.showInputDialog("Enter price:"));
return price;
}	

int getNumOfCopies()
{
NumOfCopies = Integer.parseInt(JOptionPane.showInputDialog("Enter price:"));
return NumOfCopies;
}	

// End of get Methods 


//Start of authorCount Method

int getAuthorCount()
{
while (author1!=null)
{
authorCount++;
}
if (author2!=null)
{
authorCount++;
}
else if (author3!=null)
{
authorCount++;
}
else if (author4!=null)
{
authorCount++;
}


return authorCount;
}
//End of authorCount Method 


//EOF
}







import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class bookTest 
{
public static void main(String[]args)
{
String temp; 

book getTitleObject = new book(); 

book getPublisherObject = new book(); 

book getIsbnObject = new book(); 

book getCopyRightObject = new book(); 

book getAuthor1Object = new book(); 

book getAuthor2Object = new book(); 

book getAuthor3Object = new book(); 

book getAuthor4Object = new book();

book getPriceObject = new book(); 
book getAuthorCountObject = new book();
book getNumOfCopiesObject = new book();

temp = 
"This is your Title: "+getTitleObject.getTitle()+
"\n Publisher: "+getPublisherObject.getPublisher()+
"\n ISBN: "+getIsbnObject.getIsbn()+
"\n Copy Right: "+getCopyRightObject.getCopyRight()+
"\n Author1: "+getAuthor1Object.getAuthor1()+
"\n Author2: "+getAuthor2Object.getAuthor2()+
"\n Author3: "+getAuthor3Object.getAuthor3()+
"\n Author4: "+getAuthor2Object.getAuthor4()+
"\n Price: "+String.format("%.2f", getPriceObject.getPrice())+
"\n Num of Authors: "+getAuthorCountObject.getAuthorCount()+
"\n Num of Copies: " +getNumOfCopiesObject.getNumOfCopies(); 

JOptionPane.showMessageDialog(null,temp, " ",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0); 



}
}





so now my only issues are: getting the damn getAuthorCount to work properly because it's not, for some reason...thought not sure why (i've tried many ways including boolean and it don't work). Also, the next things would be to create a search function and to make sure that it updates it self and to create an array of book [100}. If anyone can help, it'd be nice. Thank you guys.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 20 April 2010 - 11:44 AM

In the future, please properly indent your code as well. It is hard for me to follow the logic of your code.

As for your problem, you are testing while(author1 != null), without modifying the condition inside the loop so it becomes false. Each author variable will always be set or null- so this creates an infinite loop and will cause overflow in your count variable (meaning that the count will exceed the maximum value for int and start back over at Integer.MIN_VALUE). I think if statements would be more appropriate than loops here.
int getAuthorCount() 
{ 
while (author1!=null) 
{ 
authorCount++; 
} 
if (author2!=null) 
{ 
authorCount++; 
} 
else if (author3!=null) 
{ 
authorCount++; 
} 
else if (author4!=null) 
{ 
authorCount++; 
} 
 

Was This Post Helpful? 0
  • +
  • -

#8 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 20 April 2010 - 05:05 PM

View Postmacosxnerd101, on 20 April 2010 - 09:44 AM, said:

In the future, please properly indent your code as well. It is hard for me to follow the logic of your code.

As for your problem, you are testing while(author1 != null), without modifying the condition inside the loop so it becomes false. Each author variable will always be set or null- so this creates an infinite loop and will cause overflow in your count variable (meaning that the count will exceed the maximum value for int and start back over at Integer.MIN_VALUE). I think if statements would be more appropriate than loops here.
int getAuthorCount() 
{ 
while (author1!=null) 
{ 
authorCount++; 
} 
if (author2!=null) 
{ 
authorCount++; 
} 
else if (author3!=null) 
{ 
authorCount++; 
} 
else if (author4!=null) 
{ 
authorCount++; 
} 
 



Right, but how would you fix it? that's just the same code i gave earlier

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 20 April 2010 - 05:08 PM

Code tags are for code, not questions.

I think I gave you some suggestions if you re-read my post. Yes- I'm well aware that I did not modify your code. I believe you have enough information to solve the problem now.
Was This Post Helpful? 0
  • +
  • -

#10 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 20 April 2010 - 08:45 PM

i am not sure what you mean with what you typed? i corrected the other code and now it works properly, minus the method authorCount. now i just need to create an array and try date inside for it. as part c on the sheet says. If anyone is able to help in figuring out how to create an array for this class and create a search option, that would be nice.. Thank you guys.


Here is my newest code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class Book
{
static Scanner console = new Scanner(System.in);	
private	String title; 
private	String publisher; 
private	String isbn;
private	String copyRight; 
private	String author1; 
private	String author2; 
private	String author3; 
private	String author4;
private  int 	 copies;
private	double price;
private	int 	 authorCount; 

//contstructor 
void book(String constTitle, 
String constPublisher, String constIsbn,String constCopyRight,
String constAuthor1 , String constAuthor2,String constAuthor3, 
String constAuthor4, int constCopies,double constPrice,int constAuthorCount) 
	{

title = constTitle;
publisher = constPublisher; 
isbn = constIsbn;
copyRight = constCopyRight;
author1 = constAuthor1; 
author2 = constAuthor2; 
author3 = constAuthor3;
author4 = constAuthor4;
copies = constCopies; 
price = constPrice;
authorCount = constAuthorCount;
	}
	
	
// Start of Set Method 	
void setTitle(String constTitle)
			{
title = constTitle;
			} 	

void setPublisher(String constPublisher)
			{
publisher = constPublisher;
			} 

void setIsbn(String constIsbn)
			{
isbn = constIsbn;
			} 

void setCopyRight(String constCopyRight)
			{
copyRight = constCopyRight;
			} 

void setAuthor1(String constAuthor1)
			{
author1 = constAuthor1;
			} 
			
void setAuthor2(String constAuthor2)
{
    author2 = constAuthor2;
} 
			
void setAuthor3(String constAuthor3)
{
       author3 = constAuthor3;
}
			
void setAuthor4(String constAuthor4)
			{
author4 = constAuthor4;
			}
			
void setCopies(int constCopies)       
			{
copies = constCopies;
			}			
			
void setPrice(float constPrice)
			{
price = constPrice;
			}			 	
			
void setAuthorCount(int constAuthorCount)
			{
authorCount = constAuthorCount;
			}
	 						
// End of Set Method 


// Start of Get Methods 
public String getTitle()
			{
title = JOptionPane.showInputDialog("Enter title: ");			
return title;
			}
			
String getPublisher()
			{
publisher = JOptionPane.showInputDialog("Enter publisher: "); 
return publisher;
			}				
	
String getIsbn()
			{
isbn = JOptionPane.showInputDialog("Enter ISBN: "); 
return isbn;
			}

String getCopyRight()
			{
copyRight = JOptionPane.showInputDialog("Enter copy right: "); 
return copyRight;
			}

String getAuthor1()
			{
author1 = JOptionPane.showInputDialog("Enter author 1: "); 
return author1;
			}

String getAuthor2()
			{
author2 = JOptionPane.showInputDialog("Enter author 2: "); 
return author2;
			}

String getAuthor3()
			{
author3 = JOptionPane.showInputDialog("Enter author 3: ");
return author3;
			}

String getAuthor4()
			{
author4 = JOptionPane.showInputDialog("Enter author 4: ");
return author4;
			}
			
double getPrice()
			{
price = Float.parseFloat(JOptionPane.showInputDialog("Enter price: "));
return price;
			}			
			
int getCopies()
			{
copies = Integer.parseInt(JOptionPane.showInputDialog("Enter copies count: "));
return copies;
			}			

// End of get Methods 	


//Start of authorCount Method
 
int getAuthorCount()
	{
		if (author1!=null)
			{
			authorCount++;
			}
		else if (author2!=null)
			{
			authorCount++;
			}
		else if (author3!=null)
			{
			authorCount++;
			}
		else if (author4!=null)
			{
			authorCount++;
			}
				
return authorCount;
	}
//End of authorCount Method 


	//EOF
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;


public class BookTest 
{
	public static void main(String[]args)
	{
   String temp; 
	
		
	Book b1 = new Book(); 
	
	temp = 
	 "This is your Title: "+b1.getTitle()+
	 "\n Publisher: "+b1.getPublisher()+
	 "\n ISBN: "+b1.getIsbn()+
	 "\n Copy Right: "+b1.getCopyRight()+
	 "\n Author1: "+b1.getAuthor1()+
	 "\n Author2: "+b1.getAuthor2()+
	 "\n Author3: "+b1.getAuthor3()+
	 "\n Author4: "+b1.getAuthor4()+
	 "\n Price: "+String.format("%.2f", b1.getPrice())+
	 "\n Num of Authors: "+b1.getAuthorCount()+
	 "\n Num of Copies: " +b1.getCopies(); 
 	
JOptionPane.showMessageDialog(null,temp, " ",
                                   JOptionPane.INFORMATION_MESSAGE);

	  			System.exit(0); 
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 21 April 2010 - 07:03 AM

The reason your authorCount() method isn't working is b/c it modifies the authorCount instance variable on each invocation. So on the first invocation, authorCount is incremented by up to 4 from the value you set it to. On the second invocation, it is incremented by up to 4 again. So rather than recalculating the number of authors each time the method is called, why don't you just return the instance variable authorCount (I didn't realize you had a setter method for authorCount, so that changes things)?

As for working with arrays, you should check out the Java Tutorials section. TriggaMike has a good tutorial on arrays.

Also, please do properly indent your code. You can easily download the JIndent plugin for NetBeans that will format your code for you. Right now, I can't easily follow your code. If you want us to help you, please help us by indenting your code.
Was This Post Helpful? 0
  • +
  • -

#12 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 21 April 2010 - 10:07 AM

View Postmacosxnerd101, on 21 April 2010 - 05:03 AM, said:

The reason your authorCount() method isn't working is b/c it modifies the authorCount instance variable on each invocation. So on the first invocation, authorCount is incremented by up to 4 from the value you set it to. On the second invocation, it is incremented by up to 4 again. So rather than recalculating the number of authors each time the method is called, why don't you just return the instance variable authorCount (I didn't realize you had a setter method for authorCount, so that changes things)?

As for working with arrays, you should check out the Java Tutorials section. TriggaMike has a good tutorial on arrays.

Also, please do properly indent your code. You can easily download the JIndent plugin for NetBeans that will format your code for you. Right now, I can't easily follow your code. If you want us to help you, please help us by indenting your code.



Okay, i will get to it during my lunch... passed out trying to work on it yesterday night. I will also download the plug in.. reason i don't have the indent thing properly is that i am using jgrasp and am not familiar with netbeans... but i will download and fix. i will also look at the video. thank again, will keep you posted.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Class Book

Posted 21 April 2010 - 10:11 AM

Regardless of what IDE you are using, I'm sure you at least know how to use the space bar and tab key to indent your code.
Was This Post Helpful? 0
  • +
  • -

#14 Syed Faraz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-April 09

Re: Class Book

Posted 21 April 2010 - 10:43 AM

Nice code though
Was This Post Helpful? 0
  • +
  • -

#15 _marlon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-March 09

Re: Class Book

Posted 21 April 2010 - 12:24 PM

View Postmacosxnerd101, on 21 April 2010 - 08:11 AM, said:

Regardless of what IDE you are using, I'm sure you at least know how to use the space bar and tab key to indent your code.


I do, most of this stuff is aligned. I aligned it my self... though am not sure how far i am suppose to indent certain things. either way, i'm downloading net-beans right now and the plug in as well as reading the array tutorial... Though, my author count does not work as i expect it too, yet, this program will also save other peoples but once i'm done with it! Either way, i do appreciate your help as well as everyone else's contribution(s). So, for those that are contributing, thank you.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3