0

Movie tickets

Error missing parenthesis. There's something wrong with the constructor import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String movie = read.nextLine(); int row = read.nextInt(); int seat = read.nextInt(); Ticket ticket = new Ticket(movie, row, seat); System.out.println("Movie: " + ticket.getMovie()); System.out.println("Row: " + ticket.getRow()); System.out.println("Seat: " + ticket.getSeat()); } } class Ticket { private String movie; private int row; private int seat; //complete the constructor public Ticket(String movie, int row, int seat) { this.setMovie(String movie); this.setRow(int row); this.setSeat(int seat); } public String getMovie() { return movie; } public int getRow() { return row; } public int getSeat() { return seat; } }

22nd Nov 2021, 9:53 PM
Natanael
5 Answers
+ 1
public Ticket(String movie, int row, int seat) { this.movie = movie; this.row = row; this.seat = seat; }
22nd Nov 2021, 10:35 PM
Coding Cat
Coding Cat - avatar
+ 1
Yes, but this one has a setter function: //Setter public void setColor(String c){ this.color = c } And the constructor is calling this setter to set the color. In first one is no setter function. Here sets the constructor all 3 values directly.
23rd Nov 2021, 1:27 PM
Coding Cat
Coding Cat - avatar
0
Thank you, on a previous lesson the constructor was setup this way Vehicle(){ this.setColor("Red"); } This is why I tried it this way, this is almost like Mandarin Chinese to me at this time public class Vehicle { private String color; Vehicle() { this.setColor("Red"); } Vehicle(String c) { this.setColor(c); } // Setter public void setColor(String c) { this.color = c; } // Getter public String getColor() { return color; } } public class Program { public static void main(String[] args) { //color will be "Red" Vehicle v1 = new Vehicle(); //color will be "Green" Vehicle v2 = new Vehicle("Green"); System.out.println(v2.getColor()); } }
23rd Nov 2021, 12:56 PM
Natanael
0
Thank you, I understand it now. I see that the code with the setter and getter functions jumps around a lot. Would you say most modern programs are made using setter and getter functions?
23rd Nov 2021, 7:11 PM
Natanael
0
Hmm, setter and getter are only used in OOP programming. And they are important for that. But if you are mostly in functional programming, then OOP, setter and getter are less important. And which of both is the better way of programming? That depends on task you have to do.
23rd Nov 2021, 7:42 PM
Coding Cat
Coding Cat - avatar