implement all TODOs exept bonus #1
@@ -54,27 +54,27 @@ public class Rational {
|
||||
// TODO: Instance method - this - other
|
||||
// Formula: a/b - c/d = (a*d - c*b) / (b*d)
|
||||
public Rational subtract(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int num = this.numerator * other.denominator - this.denominator * other.numerator ;
|
||||
int den = this.denominator * other.denominator;
|
||||
return new Rational(num, den);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 - r2
|
||||
public static Rational subtract(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.subtract(r2);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this * other
|
||||
// Formula: (a/b) * (c/d) = (a*c) / (b*d)
|
||||
public Rational multiply(Rational other) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int num = this.numerator * other.numerator;
|
||||
int den = this.denominator * other.denominator;
|
||||
return new Rational(num, den);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 * r2
|
||||
public static Rational multiply(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.multiply(r2);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this / other
|
||||
@@ -83,13 +83,19 @@ public class Rational {
|
||||
// YOUR CODE HERE
|
||||
// HINT: Division is multiplication by reciprocal
|
||||
// Don't forget to check if other.numerator is zero!
|
||||
return null; // Remove this line when implemented
|
||||
if (other.numerator == 0)
|
||||
{
|
||||
throw new ArithmeticException("The divisor must not be zero");
|
||||
}
|
||||
int num = this.numerator * other.denominator;
|
||||
int den = this.denominator * other.numerator;
|
||||
return new Rational(num, den);
|
||||
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 / r2
|
||||
public static Rational divide(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
return r1.divide(r2);
|
||||
}
|
||||
|
||||
// PROVIDED - String representation
|
||||
|
||||
@@ -22,7 +22,9 @@ public class Main {
|
||||
|
||||
case 2:
|
||||
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
|
||||
|
||||
long generId = getLongInput("Enter the ID: ");
|
||||
long page = getLongInput("Enter the page: ");
|
||||
movieService.displayMoviesByGenre(generId, page);
|
||||
break;
|
||||
|
||||
|
||||
|
||||
@@ -16,9 +16,82 @@ public class Movie {
|
||||
private String imdb_rating;
|
||||
private String country;
|
||||
|
||||
|
||||
// TODO: Create a constructor with all fields
|
||||
public Movie(Long id, String title, List<String> genres, List<String> images, String year, String imdb_rating, String country) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.genres = genres;
|
||||
this.images = images;
|
||||
this.year = year;
|
||||
this.imdb_rating = imdb_rating;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
// TODO: Create a simple constructor with id and title only
|
||||
|
||||
public Movie(long id, String title)
|
||||
{
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
// TODO: Create getters and setters for all fields
|
||||
|
||||
public List<String> getGenres() {
|
||||
return genres;
|
||||
}
|
||||
|
||||
public void setGenres(List<String> genres) {
|
||||
this.genres = genres;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getImdb_rating() {
|
||||
return imdb_rating;
|
||||
}
|
||||
|
||||
public void setImdb_rating(String imdb_rating) {
|
||||
this.imdb_rating = imdb_rating;
|
||||
}
|
||||
|
||||
public List<String> getImages() {
|
||||
return images;
|
||||
}
|
||||
|
||||
public void setImages(List<String> images) {
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(String year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -106,7 +106,23 @@ public class MovieService {
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
try {
|
||||
// Your code here
|
||||
String json = getMoviesByGenreList(genreId, page);
|
||||
JsonElement j = JsonParser.parseString(json);
|
||||
JsonObject obj = j.getAsJsonObject();
|
||||
JsonArray movies = obj.getAsJsonArray("movies");
|
||||
for(int i = 0; i < movies.size(); i ++)
|
||||
{
|
||||
JsonObject movie = movies.get(i).getAsJsonObject();
|
||||
long id = movie.get("id").getAsLong();
|
||||
String title = movie.get("title").getAsString();
|
||||
int year = movie.get("year").getAsInt();
|
||||
System.out.printf("ID: %d | %s (%d)\n", id, title, year);
|
||||
}
|
||||
System.out.println();
|
||||
int total = obj.get("total").getAsInt();
|
||||
int totalPages = obj.get("total_pages").getAsInt();
|
||||
System.out.printf("Total: %d\n", total);
|
||||
System.out.printf("Total pages: %d", totalPages);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
@@ -189,4 +205,6 @@ public class MovieService {
|
||||
System.err.println("ERROR parsing movie details: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user