fix: added implementations

This commit is contained in:
DesertAndSand
2026-04-25 12:31:58 +03:30
parent 5473658dc8
commit 406da4c211
5 changed files with 477 additions and 129 deletions
+59 -40
View File
@@ -1,108 +1,127 @@
package Rational; package Rational;
public class Rational { public class Rational
{
private int numerator; private int numerator;
private int denominator; private int denominator;
// Constructor // Constructor
public Rational(int numerator, int denominator) { public Rational(int numerator, int denominator)
if (denominator == 0) { {
if (denominator == 0)
throw new IllegalArgumentException("Denominator cannot be zero"); throw new IllegalArgumentException("Denominator cannot be zero");
}
this.numerator = numerator; this.numerator = numerator;
this.denominator = denominator; this.denominator = denominator;
simplify(); simplify();
} }
// GCD helper for simplification // GCD helper for simplification
private int gcd(int a, int b) { private int gcd(int a, int b)
{
a = Math.abs(a); a = Math.abs(a);
b = Math.abs(b); b = Math.abs(b);
while (b != 0) {
while (b != 0)
{
int temp = b; int temp = b;
b = a % b; b = a % b;
a = temp; a = temp;
} }
return a; return a;
} }
// Simplify the fraction // Simplify the fraction
private void simplify() { private void simplify()
{
int gcd = gcd(numerator, denominator); int gcd = gcd(numerator, denominator);
numerator /= gcd; numerator /= gcd;
denominator /= gcd; denominator /= gcd;
// Keep denominator positive // Keep denominator positive
if (denominator < 0) { if (denominator < 0)
{
numerator = -numerator; numerator = -numerator;
denominator = -denominator; denominator = -denominator;
} }
} }
// COMPLETED - Instance method: this + other // COMPLETED - Instance method: this + other
public Rational add(Rational other) { public Rational add(Rational other)
{
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator; int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator; int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator); return new Rational(newNumerator, newDenominator);
} }
// COMPLETED - Static method: r1 + r2 // COMPLETED - Static method: r1 + r2
public static Rational add(Rational r1, Rational r2) { public static Rational add(Rational r1, Rational r2)
{
return r1.add(r2); return r1.add(r2);
} }
// TODO: Instance method - this - other // Instance method - this - other
// Formula: a/b - c/d = (a*d - c*b) / (b*d) // Formula: a/b - c/d = (a*d - c*b) / (b*d)
public Rational subtract(Rational other) { public Rational subtract(Rational other)
// YOUR CODE HERE {
return null; // Remove this line when implemented int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
} }
// TODO: Static method - r1 - r2 // Static method - r1 - r2
public static Rational subtract(Rational r1, Rational 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 // Instance method - this * other
// Formula: (a/b) * (c/d) = (a*c) / (b*d) // Formula: (a/b) * (c/d) = (a*c) / (b*d)
public Rational multiply(Rational other) { 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 // Static method - r1 * r2
public static Rational multiply(Rational r1, Rational 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 // Instance method - this / other
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c) // Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
public Rational divide(Rational other) { public Rational divide(Rational other)
// YOUR CODE HERE {
// HINT: Division is multiplication by reciprocal if (other.numerator == 0)
// Don't forget to check if other.numerator is zero! throw new ArithmeticException("Cannot divide by zero");
return null; // Remove this line when implemented
int newNumerator = this.numerator * other.denominator;
int newDenominator = this.denominator * other.numerator;
return new Rational(newNumerator, newDenominator);
} }
// TODO: Static method - r1 / r2 // Static method - r1 / r2
public static Rational divide(Rational r1, Rational 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 // PROVIDED - String representation
@Override @Override
public String toString() { public String toString()
if (denominator == 1) { {
if (denominator == 1)
return numerator + ""; return numerator + "";
}
return numerator + "/" + denominator; return numerator + "/" + denominator;
} }
// PROVIDED - Decimal value // PROVIDED - Decimal value
public double toDouble() { public double toDouble()
{
return (double) numerator / denominator; return (double) numerator / denominator;
} }
} }
+15 -9
View File
@@ -1,35 +1,41 @@
package movie.apis; package movie.apis;
public class Genre { public class Genre
{
private Long id; private Long id;
private String name; private String name;
public Genre(Long id, String name) { public Genre(Long id, String name)
{
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
public Genre() { public Genre() {}
}
public Long getId() { public Long getId()
{
return id; return id;
} }
public void setId(Long id) { public void setId(Long id)
{
this.id = id; this.id = id;
} }
public String getName() { public String getName()
{
return name; return name;
} }
public void setName(String name) { public void setName(String name)
{
this.name = name; this.name = name;
} }
@Override @Override
public String toString() { public String toString()
{
return id + ". " + name; return id + ". " + name;
} }
} }
+29 -14
View File
@@ -2,57 +2,70 @@ package movie.apis;
import java.util.Scanner; import java.util.Scanner;
public class Main { public class Main
{
private static final MovieService movieService = new MovieService(); private static final MovieService movieService = new MovieService();
private static final Scanner scanner = new Scanner(System.in); private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) { public static void main(String[] args)
{
System.out.println("\n=== MOVIE API CLIENT ===\n"); System.out.println("\n=== MOVIE API CLIENT ===\n");
boolean running = true; boolean running = true;
while (running) { while (running)
{
displayMenu(); displayMenu();
int choice = getIntInput("Enter your choice: "); int choice = getIntInput("Enter your choice: ");
switch (choice) { switch (choice)
{
case 1: case 1:
{
movieService.displayGenres(); movieService.displayGenres();
break; break;
}
case 2: case 2:
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre() {
long GenreId = getLongInput("Enter genre ID: ");
long PageNumber = getLongInput("Enter page number: ");
movieService.displayMoviesByGenre(GenreId, PageNumber);
break; break;
}
case 3: case 3:
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails() {
long MovieId = getLongInput("Enter movie ID: ");
movieService.displayMovieDetails(MovieId);
break; break;
}
case 0: case 0:
{
running = false; running = false;
System.out.println("Goodbye!"); System.out.println("Goodbye!");
break; break;
}
default: default:
System.out.println("Invalid choice! Please enter 0-4"); System.out.println("Invalid choice! Please enter 0-4");
} }
if (running) { if (running)
{
System.out.println("\nPress Enter to continue..."); System.out.println("\nPress Enter to continue...");
scanner.nextLine(); scanner.nextLine();
scanner.nextLine();
} }
} }
scanner.close(); scanner.close();
} }
private static void displayMenu() { private static void displayMenu()
{
System.out.println("\n================================"); System.out.println("\n================================");
System.out.println(" MENU"); System.out.println(" MENU");
System.out.println("================================"); System.out.println("================================");
@@ -63,7 +76,8 @@ public class Main {
System.out.println("================================"); System.out.println("================================");
} }
private static int getIntInput(String prompt) { private static int getIntInput(String prompt)
{
System.out.print(prompt); System.out.print(prompt);
while (!scanner.hasNextInt()) { while (!scanner.hasNextInt()) {
System.out.print("Invalid input. " + prompt); System.out.print("Invalid input. " + prompt);
@@ -74,7 +88,8 @@ public class Main {
return input; return input;
} }
private static Long getLongInput(String prompt) { private static Long getLongInput(String prompt)
{
System.out.print(prompt); System.out.print(prompt);
while (!scanner.hasNextLong()) { while (!scanner.hasNextLong()) {
System.out.print("Invalid input. " + prompt); System.out.print("Invalid input. " + prompt);
+281 -8
View File
@@ -6,19 +6,292 @@ import java.util.List;
* Model class representing a movie * Model class representing a movie
* Fields: id, title, genres, images * Fields: id, title, genres, images
*/ */
public class Movie { public class Movie
{
private Long id; private Long id;
private String title; private String title;
private List<Genre> genres;
private String images;
private String year; private String year;
private String imdbRating; private String imdb_id;
private String imdb_rating;
private String metascore;
private String imdb_votes; // Moved closer to ratings
private String poster;
private List<String> images;
private String plot;
private List<String> genres;
private String director;
private String writer;
private String actors;
private String country; private String country;
private String released;
private String runtime;
private String rated;
private String awards;
private String type;
// TODO: Create a constructor with all fields // constructor with all fields
public Movie(Long id,
String title,
List<String> genres,
List<String> images,
String year,
String imdb_rating,
String poster,
String country)
{
this.id = id;
this.title = title;
this.genres = genres;
this.images = images;
this.year = year;
this.imdb_rating = imdb_rating;
this.poster = poster;
this.country = country;
}
// TODO: Create a simple constructor with id and title only // 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 // getters and setters for all fields
public void setWriter(String writer)
{
this.writer = writer;
}
public void setType(String type)
{
this.type = type;
}
public void setRuntime(String runtime)
{
this.runtime = runtime;
}
public void setReleased(String released)
{
this.released = released;
}
public void setRated(String rated)
{
this.rated = rated;
}
public void setPlot(String plot)
{
this.plot = plot;
}
public void setMetascore(String metascore)
{
this.metascore = metascore;
}
public void setImdb_votes(String imdb_votes)
{
this.imdb_votes = imdb_votes;
}
public void setImdb_id(String imdb_id)
{
this.imdb_id = imdb_id;
}
public void setDirector(String director)
{
this.director = director;
}
public void setAwards(String awards)
{
this.awards = awards;
}
public void setActors(String actors)
{
this.actors = actors;
}
public void setId(Long id)
{
this.id = id;
}
public void setTitle(String title)
{
this.title = title;
}
public void setGenres(List<String> genres)
{
this.genres = genres;
}
public void setImages(List<String> images)
{
this.images = images;
}
public void setPoster(String poster)
{
this.poster = poster;
}
public void setYear(String year)
{
this.year = year;
}
public void setImdb_rating(String imdb_rating)
{
this.imdb_rating = imdb_rating;
}
public void setCountry(String country)
{
this.country = country;
}
public String getWriter()
{
return writer;
}
public String getType()
{
return type;
}
public String getRuntime()
{
return runtime;
}
public String getReleased()
{
return released;
}
public String getRated()
{
return rated;
}
public String getPlot()
{
return plot;
}
public String getImdb_votes()
{
return imdb_votes;
}
public String getMetascore()
{
return metascore;
}
public String getImdb_id()
{
return imdb_id;
}
public String getDirector()
{
return director;
}
public String getAwards()
{
return awards;
}
public String getActors()
{
return actors;
}
public Long getId()
{
return id;
}
public String getTitle()
{
return title;
}
public List<String> getGenres()
{
return genres;
}
public List<String> getImages()
{
return images;
}
public String getYear()
{
return year;
}
public String getImdb_rating()
{
return imdb_rating;
}
public String getPoster()
{
return poster;
}
public String getCountry()
{
return country;
}
@Override
public String toString()
{
StringBuilder MovieInfo = new StringBuilder();
MovieInfo.append("=-=-=-=-= Movie Information: =-=-=-=-=\n");
MovieInfo.append("- Title: ").append(title).append("\n");
MovieInfo.append("- Year: ").append(year).append("\n");
MovieInfo.append("- Runtime: ").append(runtime).append("\n");
MovieInfo.append("- Rated: ").append(rated).append("\n");
MovieInfo.append("- Released: ").append(released).append("\n");
MovieInfo.append("- IMDB Rating: ").append(imdb_rating).append("/10 (").append(imdb_votes).append(" votes)\n");
MovieInfo.append("- Metascore: ").append(metascore).append("/100\n");
MovieInfo.append("\n");
MovieInfo.append("- Director(s): ").append(director).append("\n");
MovieInfo.append("- Writer(s): ").append(writer).append("\n");
MovieInfo.append("- Actor(s): ").append(actors).append("\n");
MovieInfo.append("\n");
MovieInfo.append("- Genres: ").append(String.join(",", genres)).append("\n");
MovieInfo.append("- Plot: ").append(plot).append("\n");
MovieInfo.append("\n");
MovieInfo.append("- Country: ").append(country).append("\n");
MovieInfo.append("- Awards: ").append(awards).append("\n");
MovieInfo.append("\n");
if (!images.isEmpty())
{
MovieInfo.append("- Screenshot(s):\n");
for (String ss : images)
MovieInfo.append(" - ").append(ss).append("\n");
}
return MovieInfo.toString();
}
} }
+93 -58
View File
@@ -4,6 +4,7 @@ import com.google.gson.*;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
@@ -12,65 +13,74 @@ import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MovieService { public class MovieService
{
private final static String GENRES_URL = "https://api.meshcomp.ir/api/v1/genres"; private final static String GENRES_URL = "https://api.meshcomp.ir/api/v1/genres";
private final static String MOVIE_BY_ID_URL = "https://api.meshcomp.ir/api/v1/movies/"; private final static String MOVIE_BY_ID_URL = "https://api.meshcomp.ir/api/v1/movies/";
private final static HttpClient client = HttpClient.newHttpClient(); private final static HttpClient client = HttpClient.newHttpClient();
private String getGenresList() { private String getGenresList()
try { {
HttpRequest request = HttpRequest.newBuilder() try
.uri(URI.create(GENRES_URL)) {
.build(); HttpRequest request = HttpRequest.newBuilder().uri(URI.create(GENRES_URL)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) { if (response.statusCode() == 200)
{
System.out.println("Genres List received successfully"); System.out.println("Genres List received successfully");
return response.body(); return response.body();
} else {
throw new IOException("HTTP error code: " + response.statusCode());
} }
} catch (Exception e) { else
throw new IOException("HTTP error code: " + response.statusCode());
}
catch (Exception e)
{
System.out.println("!!Exception : " + e.getMessage()); System.out.println("!!Exception : " + e.getMessage());
} }
return null; return null;
} }
private String getMoviesByGenreList(Long genreId, Long page) { private String getMoviesByGenreList(Long genreId, Long page)
try { {
try
{
String url = "https://api.meshcomp.ir/api/v1/genres/" + genreId + "/movies?page=" + page; String url = "https://api.meshcomp.ir/api/v1/genres/" + genreId + "/movies?page=" + page;
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
.uri(URI.create(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) { if (response.statusCode() == 200)
return response.body(); return response.body();
} else { else
{
System.out.println("HTTP error code: " + response.statusCode()); System.out.println("HTTP error code: " + response.statusCode());
return null; return null;
} }
} catch (Exception e) { }
catch (Exception e)
{
System.out.println("!!Exception : " + e.getMessage()); System.out.println("!!Exception : " + e.getMessage());
} }
return null; return null;
} }
public void displayGenres()
{
public void displayGenres() {
String jsonResponse = getGenresList(); String jsonResponse = getGenresList();
if (jsonResponse == null) { if (jsonResponse == null)
{
System.out.println("Failed to get genres"); System.out.println("Failed to get genres");
return; return;
} }
try { try
{
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject(); JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonArray genresArray = rootObject.getAsJsonArray("genres"); JsonArray genresArray = rootObject.getAsJsonArray("genres");
Type genreListType = new TypeToken<ArrayList<Genre>>(){}.getType(); Type genreListType = new TypeToken<ArrayList<Genre>>(){}.getType();
@@ -78,17 +88,19 @@ public class MovieService {
List<Genre> genres = gson.fromJson(genresArray, genreListType); List<Genre> genres = gson.fromJson(genresArray, genreListType);
System.out.println("\nALL GENRES:"); System.out.println("\nALL GENRES:");
for (Genre genre : genres) { for (Genre genre : genres)
System.out.println(genre.getId() + ". " + genre.getName()); System.out.println(genre.getId() + ". " + genre.getName());
}
System.out.println(); System.out.println();
} catch (Exception e) { }
catch (Exception e)
{
System.err.println("ERROR parsing genres: " + e.getMessage()); System.err.println("ERROR parsing genres: " + e.getMessage());
} }
} }
/** /**
* TODO 1: Display movies for a specific genre and page * Display movies for a specific genre and page
* *
* JSON format: { "movies": [ { "id": 1, "title": "Movie", "year": "1994" } ], "total": 177, "total_pages": 18 } * JSON format: { "movies": [ { "id": 1, "title": "Movie", "year": "1994" } ], "total": 177, "total_pages": 18 }
* *
@@ -101,13 +113,30 @@ public class MovieService {
* *
* PUT ALL PARSING CODE INSIDE try-catch * PUT ALL PARSING CODE INSIDE try-catch
*/ */
public void displayMoviesByGenre(Long genreId, Long page) { public void displayMoviesByGenre(Long genreId, Long page)
// TODO: Write your code here {
// All parsing code must be inside try-catch try
try { {
// Your code here String RawData = getMoviesByGenreList(genreId, page);
if (RawData == null)
throw new IllegalArgumentException("Invalid genreId or page number; no data found");
} catch (Exception e) { JsonObject JsonData = JsonParser.parseString(RawData).getAsJsonObject();
JsonArray MoviesArray = JsonData.getAsJsonArray("movies");
Type MovieListType = new TypeToken<ArrayList<Movie>>(){}.getType();
Gson GsonHandle = new Gson();
List<Movie> Movies = GsonHandle.fromJson(MoviesArray, MovieListType);
System.out.printf("\nMovies with %s genre:\n", JsonData.getAsJsonObject("genre").get("name").getAsString());
for (Movie CurrentMovie : Movies)
System.out.printf("ID: %d | %s (%s)\n", CurrentMovie.getId(), CurrentMovie.getTitle(), CurrentMovie.getYear());
System.out.printf("\nTotal: %s - Total pages: %s", JsonData.get("total").getAsString(), JsonData.get("total_pages").getAsString());
System.out.println();
}
catch (Exception e)
{
System.err.println("ERROR: " + e.getMessage()); System.err.println("ERROR: " + e.getMessage());
} }
} }
@@ -117,18 +146,25 @@ public class MovieService {
* BONUS: Fetch movie details from API using movie ID * BONUS: Fetch movie details from API using movie ID
* URL format: https://api.meshcomp.ir/api/v1/movies/{movieId} * URL format: https://api.meshcomp.ir/api/v1/movies/{movieId}
*/ */
private String getMovieById(Long movieId) { private String getMovieById(Long movieId)
try { {
// TODO: Complete this method try
// String url = MOVIE_BY_ID_URL + movieId; {
// HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build(); final String URL = MOVIE_BY_ID_URL + movieId;
// HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// if (response.statusCode() == 200) return response.body(); HttpRequest Request = HttpRequest.newBuilder().uri(URI.create(URL)).build();
HttpResponse<String> Respone = client.send(Request, HttpResponse.BodyHandlers.ofString());
if (Respone.statusCode() == HttpURLConnection.HTTP_OK)
return Respone.body();
return null; return null;
} catch (Exception e) { }
catch (Exception e)
{
System.out.println("!!Exception : " + e.getMessage()); System.out.println("!!Exception : " + e.getMessage());
} }
return null; return null;
} }
@@ -158,30 +194,29 @@ public class MovieService {
* *
* @param movieId The ID of the movie to display * @param movieId The ID of the movie to display
*/ */
public void displayMovieDetails(Long movieId) { public void displayMovieDetails(Long movieId)
// Step 1: Get JSON response from API {
String jsonResponse = getMovieById(movieId); String jsonResponse = getMovieById(movieId);
// Step 2: Check if we got valid data if (jsonResponse == null)
if (jsonResponse == null) { {
System.out.println("Failed to get movie details for ID: " + movieId); System.out.println("Failed to get movie details for ID: " + movieId);
return; return;
} }
// Step 3-6: Parse JSON and display details (PUT YOUR CODE INSIDE try-catch) try
try { {
// TODO: Parse jsonResponse to JsonObject JsonObject JsonData = JsonParser.parseString(jsonResponse).getAsJsonObject();
// TODO: Extract id, title, year, genres array, poster, country, imdb_rating
// TODO: Convert genres array to a readable string
// TODO: Print all movie details
// HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
// HINT: Use getAsJsonArray("genres") to get genres
// HINT: For each genre, use getAsString()
// Your code here: Gson GsonHandle = new Gson();
Type MovieType = new TypeToken<Movie>(){}.getType();
Movie RequestedMovie = GsonHandle.fromJson(JsonData, MovieType);
System.out.println();
} catch (Exception e) { System.out.println(RequestedMovie);
}
catch (Exception e)
{
System.err.println("ERROR parsing movie details: " + e.getMessage()); System.err.println("ERROR parsing movie details: " + e.getMessage());
} }
} }