HW03
This commit is contained in:
@@ -28,5 +28,17 @@
|
||||
<version>2.17.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>25</source>
|
||||
<target>25</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -54,27 +54,31 @@ 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 newNumerator= this.numerator*other.denominator-this.denominator*other.numerator;
|
||||
int newDenominator= this.denominator*other.denominator;
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 - r2
|
||||
public static Rational subtract(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNumerator= r1.numerator*r2.denominator-r1.denominator*r2.numerator;
|
||||
int newDenominator=r1.denominator*r2.denominator;
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
}
|
||||
|
||||
// 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 newNumerator=this.numerator*other.numerator;
|
||||
int newDenominator=this.denominator*other.denominator;
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 * r2
|
||||
public static Rational multiply(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNumerator=r1.numerator*r2.numerator;
|
||||
int newDenominator= r1.denominator*r2.denominator;
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
}
|
||||
|
||||
// TODO: Instance method - this / other
|
||||
@@ -83,13 +87,29 @@ 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
|
||||
int newNumerator;
|
||||
int newDenominator;
|
||||
if (other.numerator==0){
|
||||
throw new IllegalArgumentException(other+" should not be zero");
|
||||
}
|
||||
else{
|
||||
newNumerator= this.numerator*other.denominator;
|
||||
newDenominator=this.denominator*other.denominator;
|
||||
}
|
||||
return new Rational(newNumerator, newDenominator);
|
||||
}
|
||||
|
||||
// TODO: Static method - r1 / r2
|
||||
public static Rational divide(Rational r1, Rational r2) {
|
||||
// YOUR CODE HERE
|
||||
return null; // Remove this line when implemented
|
||||
int newNumerator; int newDenominator;
|
||||
if (r2.numerator==0){
|
||||
throw new IllegalArgumentException(r2.numerator+" shouldn't be zero");
|
||||
}
|
||||
else{
|
||||
newNumerator=r1.numerator*r2.denominator;
|
||||
newDenominator=r1.denominator*r2.numerator;
|
||||
}
|
||||
return new Rational(newNumerator, newDenominator); // Remove this line when implemented
|
||||
}
|
||||
|
||||
// PROVIDED - String representation
|
||||
|
||||
@@ -22,6 +22,10 @@ public class Main {
|
||||
|
||||
case 2:
|
||||
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
|
||||
Scanner input = new Scanner(System.in);
|
||||
Long genreID= input.nextLong();
|
||||
Long page=input.nextLong();
|
||||
movieService.displayMoviesByGenre(genreID, page);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package movie.apis;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -10,15 +13,65 @@ public class Movie {
|
||||
|
||||
private Long id;
|
||||
private String title;
|
||||
@JsonProperty("genre")
|
||||
private List<String> genres;
|
||||
private List<String> images;
|
||||
private String year;
|
||||
private String imdb_rating;
|
||||
private String country;
|
||||
|
||||
public Movie(){};
|
||||
// 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 Long idGetter(){
|
||||
return id;
|
||||
}
|
||||
public String titleGetter(){
|
||||
return title;
|
||||
}
|
||||
public List<String> genresGetter(){
|
||||
return genres;
|
||||
}
|
||||
public List<String> imagesGetter(){
|
||||
return images;
|
||||
}
|
||||
public String yearGetter(){
|
||||
return year;
|
||||
}
|
||||
public String Imdb_ratingGetter(){
|
||||
return imdb_rating;
|
||||
}
|
||||
public String countryGetter(){
|
||||
return country;
|
||||
}
|
||||
public void setId(Long id){
|
||||
this.id=id;
|
||||
}
|
||||
public void setTitle(String title){
|
||||
this.title=title;
|
||||
}
|
||||
public void setGenres(List<String> genres){
|
||||
this.genres= new ArrayList<>(genres);
|
||||
}
|
||||
public void setImages(List<String> images){
|
||||
this.images= new ArrayList<>(images); //for privacy stuff
|
||||
}
|
||||
public void setYear(String year){
|
||||
this.year=year;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public void setImdb_rating(String imdb_rating) {
|
||||
this.imdb_rating = imdb_rating;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package movie.apis;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
@@ -106,12 +108,16 @@ public class MovieService {
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
try {
|
||||
// Your code here
|
||||
|
||||
} catch (Exception e) {
|
||||
String json= getMoviesByGenreList(genreId, page);
|
||||
ObjectMapper mapper= new ObjectMapper();
|
||||
Movie m = mapper.readValue(json, Movie.class);
|
||||
System.out.println("ID: " + m.idGetter() + "\nTitle: " + m.titleGetter() + "\nYear: " + m.yearGetter());
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user