Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to implement this singleton class in java . How can I
put these variables in this class?
This is one simple example for your singleton. Feel free to add setters and getters as you need it for the fields.
I'm not sure what the set-method in your diagram should do but maybe you don't need it anyway.
public class LibraryInfo {
private static final LibraryInfo instance = new LibraryInfo();
public static LibraryInfo getInstance() {
return instance;
}
private LibraryInfo() {}
private String name;
private int phone;
private String address;
private String openTime;
private String closeTime;
// getters
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am relatively new to java and don't understand why the setter method is a static context when I haven't used "static" anywhere.
public class Appointment {
LocalDateTime Time;
Doctor Doctor;
Patient Patient;
String Notes;
public Appointment(LocalDateTime time, Doctor doc, Patient pat, String notes){
Time = time;
Doctor = doc;
Patient = pat;
Notes = notes;
}
public void setNotes(String Notes) {
Appointment.Notes = Notes;
}
}
EDIT: Someone commented the answer so I cant mark it as correct but I put Appointment.Notes instead of this.Notes
This is the syntax for assigning a static variable (not necessarily a member of the current class).
public class Appointment {
Appointment.Notes = Notes;
Instead write:
this.Notes = Notes;
Or better change the variable name to something standard:
this.notes = notes;
I strongly suggest sticking to naming conventions. Also it is useful to quote the actual compiler error message in questions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to print the attributes from TestCar class by creating a public hackCar method in class Terminal. The hackCar method needs to take a TestCar as a parameter and print the attributes of TestCar. The caveat for this assignment is that I cannot touch anything in the TestCar class.
I am still struggling with printing the two private attributes in TestCar. How can I print the two private attributes from Test Car class by using the Test Car object as the parameter in the hackCar method?
Story class:
class Story {
public static void main(String args[]) {
TestCar testCar = new TestCar();
Terminal terminal = new Terminal();
terminal.hackCar(testCar);
}
}
class Terminal {
public void hackCar(TestCar other) {
System.out.println(other.doorUnlockCode);
System.out.println(other.hasAirCondition);
System.out.println(other.brand);
System.out.println(other.licensePlate);
}
}
class TestCar {
private int doorUnlockCode = 602413;
protected boolean hasAirCondition = false;
String brand = "TurboCarCompany";
public String licensePlate = "PHP-600";
}
Thanks!
Private fields are called 'private' because there is no way to get them. But you can make public getter for them:
class TestCar {
// Your 4 fields here...
public int getDoorUnlockCode() {
return this.doorUnlockCode;
}
}
Then in hackCar method change
System.out.println(other.doorUnlockCode); to this: System.out.println(other.getDoorUnlockCode());
So now you can access field doorUnlockCode through public getter.
Do the same for protected field hasAirCondition
Your methods Terminal.getdoorUnlockCode() and Terminal.getAirCondition() can't get to fields from another object, they must be in TestCar object
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to create a basic Item class hierarchy.
Each Item will have a String name and an int itemID.
These variables will be set in each subclass of Item.
The Item class declares the two variables and each subclass of Item
(public class Hammer extends Item) will set the name and itemID.
For example, name = "hammer" and itemID = 01.
How would I do this efficiently and effectively?
The base class will declare two final private fields (name and itemid).
The values for these will be set via the base class constructor.
Each of the derived classes will call the base class constructor with the appropriate name and itemid value.
for example:
public abstract class Blam
{
private final int itemId;
private final String name;
protected(
final int itemId,
final String name)
{
this.itemId = itemId;
this.name = name;
}
}
public class Derived
extends
Blam
{
public Derived()
{
super(123, "nameValue");
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a a class that its like a wrapper for a map. It is a property list, mostly use for the sake of the name it looks like the following:
public class MyPropertyList implements Serializable
{
private static final long serialVersionUID = 7896123434L;
private LinkedHashMap<String,String> map;
public static final String key1 = "key1";
public static final String key2 = "key2";
//More key values
public MyPropertyList(){
map = new LinkedHashMap<String,String>();
}
public void addProperty(String key, String value){
map.put(key,value);
}
public LinkedHashMap<String,String> getMapping(){
return map;
}
public int getSize(){
return map.size;
}
//...Other delegated calls
}
It it a bad design? Does it present any issues? Is there a better design for that fulfills this purpose?
Use the Properties class from Java standar Library, which implements Serializable.
The code you have posted offers no additional functionality over LinkedHashMap, so any code which uses this might just as well use LinkedHashMap directly.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a class called GoogleWeather, I want to convert it to another class CustomWeather.
Is there any design pattern which helps you to convert classes?
In that case I'd use a Mapper class with a bunch of static methods:
public final class Mapper {
public static GoogleWeather from(CustomWeather customWeather) {
GoogleWeather weather = new GoogleWeather();
// set the properties based on customWeather
return weather;
}
public static CustomWeather from(GoogleWeather googleWeather) {
CustomWeather weather = new CustomWeather();
// set the properties based on googleWeather
return weather;
}
}
So you don't have dependencies between the classes.
Sample usage:
CustomWeather weather = Mapper.from(getGoogleWeather());
There is one critical decision to make:
Do you need the object that is generated by the conversion to reflect future changes to the source object?
If you do not need such functionality, then the simplest approach is to use a utility class with static methods that create a new object based on the fields of the source object, as mentioned in other answers.
On the other hand, if you need the converted object to reflect changes to the source object, you would probably need something along the lines of the Adapter design pattern:
public class GoogleWeather {
...
public int getTemperatureCelcius() {
...
}
...
}
public interface CustomWeather {
...
public int getTemperatureKelvin();
...
}
public class GoogleWeatherAdapter implements CustomWeather {
private GoogleWeather weather;
...
public int getTemperatureKelvin() {
return this.weather.getTemperatureCelcius() + 273;
}
...
}
Besides, You can also use new Java8 feature 'Function' from java.util.function'.
More detailed explanation is provided in http://www.leveluplunch.com/java/tutorials/016-transform-object-class-into-another-type-java8/ . Kindly have a look!