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 6 years ago.
Improve this question
class Student {
private String name;
private int age;
public static String city;
Student() {
}
Student(int a) {
age = a;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
private int getAge() {
return age;
}
public void setCity(String c) {
city = c;
}
public String getCity() {
return city;
}
}
When considering the features of object orientation, which feature(s) is/are shown clearly in the program?
How can I know that above code is:
Abstraction
Encapsulation
Data hiding
Inheritance
Polymorphism
abstraction - No (you don't have any abstract members or classes in your code).
Encapsulation - yes ( Binding code and data together - the class itself) .
polymorphism - no ( no multiple functions with same names ) .
inheritance - no (There is no class that inherits this one and vice versa )
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 days ago.
Improve this question
FXCollections.observableList
Documentation said:
Note that mutation operations made directly to the underlying list are
not reported to observers of any ObservableList that wraps it.
Code for example:
public class Test {
public static void tst() {
ArrayList<Person> arrayList = new ArrayList<>();
ObservableList<Person> people = FXCollections.observableList(arrayList);
Person person = new Person("Tom");
arrayList.add(person);
person.setName("Mary");
// How update people after that
}
public static class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}
}
What are some ways to observe the change or addition of SIMPLE objects?
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 2 years ago.
Improve this question
I am working on this Java program where I am supposed to write a class called kumquat that has an integer age with getter and setters. Additionally, I need to do input validation. I can't figure out what I'm doing wrong. I'm sorry if this is really simple I'm just still really new to this.
public class Person
{
private int age;
public int getAge()
{
return age;
}
public void setAge(int newAge)
{
this.age = newAge;
}
}
and then my main
public class Kumquat
{
public static void main(int[] args)
{
Person myObj = new Person();
myObj.setAge("5");
System.out.println(myObj.getAge());
}
}
Everything is ok in the Person class. Although in your main the method Person::setAge receives an int as parameter and you're trying to pass a String in line myObj.setAge("5");. Try passing an int like myObj.setAge(5); instead.
you should write String instead of int and setAge should not be string
public static void main(String[] args)
{
Person myObj = new Person();
myObj.setAge(5);
System.out.println(myObj.getAge());
}
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 3 years ago.
Improve this question
I'm making a Card class on Java, and I want it to be as idiomatic as possible. Should I encapsulate all the fields making them private and providing getters as following:
public class Card implements Comparable<Card> {
private char suit;
private String name;
private int value;
public Card(char suit, String name, int value) {
this.suit = suit;
this.name = name;
this.value = value;
}
public char getSuit() {
return suit;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
public String toString() {
return name + " of " + suit;
}
#Override
public int compareTo(Card card) {
return Integer.compare(value, card.value);
}
}
Or since any of the fields are not going to be modified, should make public and final all the fields:
public class Card implements Comparable<Card> {
public final char suit;
public final String name;
public final int value;
public Card(char suit, String name, int value) {
this.suit = suit;
this.name = name;
this.value = value;
}
public String toString() {
return name + " of " + suit;
}
#Override
public int compareTo(Card card) {
return Integer.compare(value, card.value);
}
}
I'm reading Clean Code on the chapter of Data Structures vs OO classes, and I do not know what approach should I take in this case. Thanks in advance!
EDIT:
This class is part of a BlackJack I'm developing, and I need to access the fields from another classes.
EDIT:
This question has been put on hold, but, where does this question should be posted then? Should I move it to Code Review? I'm truly interested in knowing the opinions of more experienced programmers on this subject, but I want to post it on the right site
In theory, when you design OOP solutions, you must provide least priviledge wherever possible. Therefore, your first approach is way to go. But, I would rather implement the hashCode() to make it more clear about the uniqueness of a Card.
What you are trying to implement is called as immutable objects
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 7 years ago.
Improve this question
{"userId":"vincent","favTracks":{"favourite":"15","unFavourite":"121"}}
What can be the Java object for the above JSON String?
It really depends on how you want to map it. If you're using Jackson, for example, with the default mapping settings, your classes could look something like:
class MyObject {
private String userId;
private FavTracks favTracks;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public FavTracks getFavTracks() {
return favTracks;
}
public void setFavTracks(FavTracks favTracks) {
this.favTracks = favTracks;
}
}
class FavTracks {
private String favourite;
private String unFavourite;
public String getFavourite() {
return favourite;
}
public void setFavourite(String favourite) {
this.favourite = favourite;
}
public String getUnFavourite() {
return unFavourite;
}
public void setUnFavourite(String unFavourite) {
this.unFavourite = unFavourite;
}
}
One remark: in your current example, the favourite and unFavourite properties are of a string type. Maybe a numeric type is more suitable?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello I have the following problem: I want to create an arraylist and want to add some items.
But somehow the .add Method is not there.
import java.util.ArrayList;
public class Chairing{
private int numbers;
ArrayList<Chairs>myList = new ArrayList<Chairs>();
myList.add(5,new Chairset("10"));
}
public class Chair{
int price;
String info;
public Chair(int price, Chairset c){
this.price = price;
info = c.getInfo();
}
}
public class Chairset{
String info;
public Chairset(String id){
id = info;
}
}
For some Reasons I can't add something in my new ArrayList. The constructor for Chair needs a price and an object Chairset. Chairset needs an id.
The problem is your classes have no common type, the tightest generic bound the list can have would be Object. Either use a marker interface, or notice the similarity between Chair and Chairset and have one extend the other - giving them a common type.
Also note that the line in your code where you add to the list is not in a legal location - it must be within a method.
Try this:
public class Chairing {
private int numbers;
List<Chairset> myList = new ArrayList<Chairset>();
public void someMethod() {
myList.add(5,new Chairset("10"));
}
}
public class Chair extends Chairset {
int price;
public Chair(int price, Chairset c){
super(c.getInfo());
this.price = price;
}
}
public class Chairset {
String info;
public Chairset(String id){
id = info;
}
}