Creating a class that has integers, getter/setters, with input validation [closed] - java

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

Related

Java FX observableList. Observation scheme [closed]

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?

How to create a private array and access it by the main method in java? [closed]

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 want to create a class outside the public class but in the same file, and in that class I wanna create a private array which can only be accessed by creating object through the previous public class. I also wanna store data to that array through the public class.
I suggest you to start learning java, this will save you from asking this kind of question next time. For the moment you can find how to achieve what you are asking for in the example bellow :
public class Learning {
public static void main(String[] args) {
Course course = new Course();
List<String> materials = new ArrayList<>();
materials.add("java basic courses");
materials.add("OOP courses");
// here we use setters to set course materials and notes
course.setMaterials(materials);
course.setNotes(new int[] {19,20});
System.out.println("Display course materials");
for (String material : course.getMaterials()) {
System.out.println("Material : " + material);
}
System.out.println("Display Notes");
for (int note : course.getNotes()) {
System.out.println("Note : " + note);
}
}
}
class Course {
private List<String> materials;
private int[] notes;
public List<String> getMaterials() {
return materials;
}
public void setMaterials(List<String> materials) {
this.materials = materials;
}
public int[] getNotes() {
return notes;
}
public void setNotes(int[] notes) {
this.notes = notes;
}
}

Understanding of nested class [closed]

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 trying to understand the below code:
public class StudentManager {
public Student find(String studentID) throws StudentNotFoundException {
if (studentID.equals("123456")) {
return new Student();
} else {
throw new StudentNotFoundException(
"Could not find student with ID " + studentID);
}
}
}
public Student find(String studentID) throws StudentNotFoundException - is this public Student a nested class? And then we used find(String studentID) a method? Can anyone please help me interpret this code? I am following this link for understanding custom exception handling.
Update:
public class StudentTest {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
try {
Student student = manager.find("0000001");
} catch (StudentNotFoundException ex) {
System.err.print(ex);
}
}
}
It is not a nested class. It is a public instance method accepting a studentID parameter as a String that returns a Student and might throw a StudentNotFoundException.

Object Orientation, Inheritance, Abstraction [closed]

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 )

How to make this method use ArrayList? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Course {
private String courseName;
ArrayList<String> students = new ArrayList<>();
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;//<-- Line 15
numberOfStudents++;
}
public ArrayList getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
}
Line 15 I am getting error "Array required, but ArrayList found.
I am unsure what to do here as I am new to strings and such.
students is declared as an ArrayList. This notation
students[numberOfStudents] = student;
only works for array types. You should use
students.add(student);
Please read the javadoc for ArrayList.
You also don't need to keep a field to hold the number of students, as
students.size();
will give you that.
If you want to use ArrrayList in your program:
public void addStudent(String student) {
students.add(student);
}
public int getNumberOfStudents() {
return students.length();
}
Also then you do not require numberOfStudents variable

Categories

Resources