• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

To make object variable of one class in another class private.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is:
TestMain.java
   

BusTicket.java

       

Person.java

       

But I am getting following fail test cases:

Fail 1 -
Incorrect access specifier/modifier for person -Should be a [private]

Fail 2 -
Check whether the signature(Returntype/Argument/AccessSpecifier/MethodName) of the method setPerson is correct
Check whether the signature(Returntype/Argument/AccessSpecifier/MethodName) of the method getPerson is correct

Please help to resolve these fail test cases.
How to made set and get function for the Person object;
If make Person object private then how to use its values.

Thanks
 
Saloon Keeper
Posts: 14260
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Utkarsh Goyal wrote:How to made set and get function for the Person object;


The same way you made getters and setters for the ticket price and total amount.

If make Person object private then how to use its values.


Through the getters you just made.
 
Utkarsh Goyal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please write the sample code and display how to do it.
 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want the Person class to be private, it must be an inner class, that means it must be defined inside of another class. why does that class need to be private?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is the Person field that should be private, not the class.
 
Stephan van Hulst
Saloon Keeper
Posts: 14260
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Utkarsh Goyal wrote:Can you please write the sample code and display how to do it.


No. CodeRanch is NotACodeMill and we ask our users to DoYourOwnHomework.

The actual code that you gave us provides plenty of examples of private fields and getters and setters. Show us what you've tried, and tell us what you don't understand.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope the original poster (=OP) hasn't been waiting since 2019 foir a solution I am afraid there could be better solutions. You haven't corrected the big design problem with that code, that it is possible to create bus ticket and person instances and have them in an inconsistent state. Unfortunately it isn't good to have so much code in the main() method, either.


[Additon]I added code tags to your code: always use thase tags please. You can see how much better it looks
 
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic