So it seems like I have to make an object array of a sub-class (Bicycle).
I then add two objects to this.. and loop the array and print what each object is constructed from.
This sounds thoroughly confusing to me, and I'm unsure how to go about this.
I'll also post the rest of my code, to make more sense.
MAIN:
package javaapplication4;
public class JavaApplication4 {
public static void main(String[] args) {
Bicycle myBike = new Bicycle(1, "Haro BMX", true, "Handlebars, Tyres, Frame");
System.out.println(myBike);
}
}
package javaapplication4;
public class Implement {
String name;
boolean hasMovingParts;
String constructedFrom;
public Implement() {
}
public Implement(String name, boolean hasMovingParts, String constructedFrom) {
this.name = name;
this.hasMovingParts = hasMovingParts;
this.constructedFrom= constructedFrom;
}
public String getName() {
return name;
}
public boolean getMovingParts() {
return hasMovingParts;
}
public String getConstructedFrom(){
return constructedFrom;
}
public class Bicycle extends Implement {
public int seatNumber;
public Bicycle(int seatNumber, String name, boolean hasMovingParts, String constructedFrom) {
this.seatNumber = seatNumber; //takes the value you pass as parameter
this.name = name; // and stores it into the instance variable
this.hasMovingParts = hasMovingParts;
this.constructedFrom = constructedFrom;
}
#Override
public String toString(){
return String.format("*Vehicle Statistics* Seats: %d, Name:" +
" %s, Contains Moving Parts: %b, Materials: %s",
seatNumber, name, hasMovingParts, constructedFrom);
}
}
}
package javaapplication4;
public class Bicycle extends Implement {
public int seatNumber;
public Bicycle(int seatNumber, String name, boolean hasMovingParts, String constructedFrom) {
this.seatNumber = seatNumber;
this.name = name;
this.hasMovingParts = hasMovingParts;
this.constructedFrom = constructedFrom;
}
#Override
public String toString() {
return String.format("*Vehicle Statistics* Seats: %d, Name:" +
" %s, Contains Moving Parts: %b, Materials: %s",
seatNumber, name, hasMovingParts, constructedFrom);
}
}
Change your main method to create an array of Bicycles, then add them by the index :
public static void main(String[] args) {
Bicycle[] bicycles = new Bicycle[2];
bicycles[0] = new Bicycle(1, "Haro BMX", true, "Handlebars, Tyres, Frame");
bicycles[1] = new Bicycle(1, "Orah XMB", true, "Handlebars, Tyres, Frame");
for (Bicycle bicycle : bicycles){
System.out.println(bicycle);
}
}
Related
I got 2 classes
class Curso{
private String name;
public Curso(String nome){
this.name = nome;
}
public String getName(){
return this.name;
}
}
and
public class testaCurso{
public static void main(String[] args){
Course c1 = new Course("Computer Science");
c1.addDisciplina("AlgProgII");
c1.addDisciplina("SO");
c1.addDisciplina ("Grafos");
System.out.println(c1);
}
}
i gotta modify the Course class so that it can store the names of the Disciplina that make up the course and work for the test above with the output as shown. Consider that a course will not have a maximum of 50 subjects.
output:
Course: Computer Science,
Disciplinas:{ AlgProgII SO Grafos }
class Curso {
private String name;
// Add an list field containg the disciplinas
private final List<String> disciplinas = new ArrayList<>();
public Curso(String name){
this.name = name;
}
public String getName(){
return this.name;
}
// Add a `addDisciplina` method
public void addDisciplina(String name) {
disciplinas.add(name);
}
// Override the `toString` method
#Override
public String toString() {
return "Course: " + name + ", Disciplinas: + ", disciplinas;
}
}
We can implement toString() like the following:
public class Course {
private final String name;
private final List<String> disciplinas;
public Course(String name){
this.name = name;
this.disciplinas = new ArrayList<>();
}
public Course(String name, List<String> disciplinas){
this.name = name;
this.disciplinas = new ArrayList<>(disciplinas);
}
public void addDisciplinas(String discplina){
this.disciplinas.add(discplina);
}
#Override
public String toString() {
return "Course: " + name + ", Disciplinas: {" + disciplinas.stream().collect(Collectors.joining(" ")) +"}";
}
}
Usage:
Course course = new Course("Computer Science", Arrays.asList("AlgProgII", "SO", "Grafos"));
System.out.println(course);
Output:
Course: Computer Science, Disciplinas: {AlgProgII SO Grafos}
We have an activity that will store and display the information of an employee.
I've already created no-modifier class named Person and a public class named Employee which is the the main method. My problem is I don't know how to make the boolean in the class Person which will be used in the main method with a scanner.
class Person {
private String name;
private int contactNum;
private boolean status;
public boolean isRegular;
public String getName(){
return name;
}
public int getContactNum(){
return contactNum;
}
public void setName(String name){
this.name=name;
}
public void setContactNum(int contactNum){
this.contactNum=contactNum;
//how to make the boolean of status and isRegular?
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Person P = new Person();
System.out.println("Type employee's name, contact number");
System.out.println("Press Enter after every input");
P.setName(input.nextLine());
P.setContactNum(input.nextInt());
System.out.println("Press Y if employee is regular or N if not");
//how to use boolean here that comes from the class Person?
System.out.println("Name: "+ P.getName());
System.out.println("Contact Number: "+ P.getContactNum());
System.out.println("Status:" + this is where the user is ask to Press Y if employee is regular or N if not )//the status is if the employee is regular or not.
My suggestion for your code:
System.out.println("Press Y if employee is regular or any other key if not");
P.setRegular(input.nextLine().equalsIgnoreCase("Y"));
Your constructor
public class Person {
private String name;
private int contactNum;
private boolean status;
private boolean isRegular;
//No arg constructor
public Person() {
}
//Full arg constructor
public Person(String name, int contactNum, boolean status, boolean isRegular) {
this.name = name;
this.contactNum = contactNum;
this.status = status;
this.isRegular = isRegular;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getContactNum() {
return contactNum;
}
public void setContactNum(int contactNum) {
this.contactNum = contactNum;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public boolean isRegular() {
return isRegular;
}
public void setRegular(boolean regular) {
isRegular = regular;
}
}
Edit
I've noticed an error in the above code and fixed it. This line should be:
P.setRegular(input.next().equalsIgnoreCase("Y"));
You can print booleans as is just like any other Java primitive. `
System.out.println("Status:" + P.isRegular());
would print Status: true or Status: false.
If you want it to print Status: Yes or Status: No, you could do something like this:
System.out.println("Status: ".concat((P.isRegular())?("Yes"):("No")));
I am trying to write a program which stores information about a person in a linked list. I made a simple person class to store the name, age and addresses in the list. I would also like to store multiple addresses for EACH person, and a fact about the place in another linked list, inside the person class.
So for example, "Tara" can have a home address of "10 Central Ave" and a work address of "5 Willow street" etc. The problem is, I don't know how to have a linked list inside another.
My goal is to check whether the person's name is already on the list, and if so, add another address for them. (So that there is no repeats). I am a beginner and can really use some help.
public class Person {
private String name;
private int age;
public LinkedList <String> adresses;
public Person() {
name = "default";
age = 0;
adresses = new LinkedList<>();
}
public Person(String n, int a) {
name = n;
age = a;
}
public LinkedList<Adress> getAdresses() {
return adresses;
}
public void setAdresses(LinkedList<Adress> adresses) {
this.adresses = adresses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name+" "+age+" "+adresses;
}
}
public class Adress {
public String adress;
public String fact;
public Adress(String a, String f) {
adress = a;
fact = f;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getFact() {
return fact;
}
public void setFact(String fact) {
this.fact = fact;
}
}
public class Test {
public static void main(String[] args) {
Person Tara = new Person("Tara",35);
Person Judah = new Person("Judah",28);
Person Mark = new Person("Mark",45);
Person Seth = new Person("Seth",23);
LinkedList<Object> tester = new LinkedList<>();
tester.add(Tara);
tester.add(Judah);
tester.addLast(Mark);
tester.addLast(Seth);
System.out.println(tester);
}
}
How is about to use the next classic data structure for your project?
public class Person {
private String name
private int age;
public List<Address> addresses;
//...
}
Is it possible to protect from performing code placed in superclass constructor? In this example the output is
From Person
From Student
but I don't need to print out From Person. If I delete super(a, n); then program will not compile. Is it possible to print out only message from subclass?
class Person {
private int age;
private String name;
public Person(int a, String n) {
this.age = a;
this.name = n;
System.out.println("From Person");
}
public int getAge() {
return age;
}
public void setAge(int a) {
this.age = a;
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
}
class Student extends Person {
private String specialization;
public Student(int a, String n, String s) {
super(a, n);
specialization = s;
System.out.println("From Student");
}
public String getSpecialization() {
return specialization;
}
public void setSpecialization(String s) {
this.specialization = s;
}
}
public class Classes {
public static void main(String[] args) {
Student student_Jack = new Student(20, "Jack", "IT");
}
}
Instead of using super(a,n) you should use your methods for setting those variables that you have made.
public Student(int a, String n, String s) {
setAge(a);
setName(n);
specialization = s;
System.out.println("From Student");
}
You must also add an empty constructor in person.
public Person(){}
This will give you the same functionality, without needing to call the super constructor.
I have two classes: profesor and subject
public class Profesor {
private int numbClassroom;
public Profesor(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String ToString(){
return "Number of classroom: "+numbClassroom;
} }
The second class is:
public class Subject{
String name;
Profesor lecturer = new Profesor();
Date yearOfStudy;
public void Dodeli(Profesor p){
??????
}}
I do not know how to add professor like a lecturer to a current subject
Like this? I don't see any problem.
public void Dodeli(Profesor p){
lecturer = p;
}
Profesor lecturer = new Profesor();
No need to instantiate lecturer. Just declare it. Then have getter/setter methods for it
Then you can assign Professor to Subject
Subject subj = new Subject("OOP"); //assuming you have corresponding constructor
subj.setLecturer(new Professor()); //or if you have existing prof object
Maybe require something like this : try to encapsulate your code
public class Professor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Subject{
private String name;
private Professor professor;
private int numbClassroom;
private Date yearOfStudy;
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Professor getProfesor() {
return professor;
}
public void setProfesor(Professor profesor) {
this.professor = profesor;
}
public void Dodeli(){
System.out.println("Pofessor "+getProfesor().getName()+" is teaching "+getName()+" in Room NO :"+getNumbClassroom());
}
}
public class TestImpl {
public static void main(String arr[])
{
Subject subject = new Subject();
Professor professor = new Professor();
subject.setName("Biology");
professor.setName("MR.X");
subject.setNumbClassroom(1111);
subject.setProfesor(professor);
subject.Dodeli();
}
}