CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/Compiles even gives the null exeption requested on the re...
Karas
Level 1
Tampa
  • 02.03.2021
  • 632views
  • 5comments

Compiles even gives the null exeption requested on the requirements, still does not pass the verification, 100 attempst I quit...

Question about the task MovieFactory
Java Core,  Level 4,  Lesson 8
Resolved

Read from the console to extend the functionality by analogy:
1. Figure out what the program does.
2. All classes must be inside the Solution class.
3. Add Cartoon and Thriller classes.
4. Understand how we get a SoapOpera object using the key "soapOpera".

Following the pattern used to obtain a SoapOpera object:
5. Add to MovieFactory.getMovie the ability to get a Cartoon object using the key "cartoon".
6. Add to MovieFactory.getMovie the ability to get a Thriller object using the key "thriller".
7. Read several keys (strings) from the console.
7.1. Stop reading in strings as soon as a string is entered that does not match one of the following: "cartoon", "thriller", "soapOpera".
8. Create a Movie movie variable, and for each entered string (key):
8.1. Get an object using MovieFactory.getMovie and assign it to the variable movie.
8.2. Display the result of calling movie.getClass().getSimpleName().

Requirements:
  • The Cartoon and Thriller classes must be static and be located inside the Solution class.
  • The MovieFactory.getMovie method must return a Cartoon object if "cartoon" is passed as an argument.
  • The MovieFactory.getMovie method must return a Thriller object if "thriller" is passed as an argument.
  • The main method must read strings from the keyboard.
  • The main method should stop reading strings from the keyboard if an invalid string is entered (i.e. something other than "cartoon", "thriller", or "soapOpera").
  • For each string entered (including the invalid string), you must call the MovieFactory.getMovie method.
  • For each valid string entered, you need to display the simple name (movie.getClass().getSimpleName()) of the object types returned by the MovieFactory.getMovie method.
package com.codegym.task.task14.task1414; import java.util.ArrayList; import java.io.*; /* MovieFactory */ public class Solution { public static void main(String[] args) throws Exception { ArrayList<Movie> cinema = new ArrayList<Movie>(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true){ String s = reader.readLine(); if (s.equals("soapOpera")||s.equals("cartoon")||s.equals("thriller")){ cinema.add(MovieFactory.getMovie(s)); } else { cinema.add(MovieFactory.getMovie(s)); break; } } for(Movie m : cinema){ System.out.println(m.getClass().getSimpleName()); } } static class MovieFactory { static Movie getMovie(String key) { Movie movie = null; // Create a SoapOpera object for the key "soapOpera" if ("soapOpera".equals(key)) { movie = new SoapOpera(); } if ("thriller".equals(key)){ movie = new thriller(); } if ("cartoon".equals(key)){ movie = new cartoon(); } return movie; } } static abstract class Movie { } static class SoapOpera extends Movie { } static class cartoon extends Movie{ } static class thriller extends Movie{ } }
0
Comments (5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Karas Backend Developer
2 March 2021, 18:02
All in correct Capitalization. Thanks.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
2 March 2021, 02:02
There is no condition that says null pointer. It says that "For each valid string entered, you need to display the simple name"
0
Guadalupe Gagnon
Level 37 , Tampa, United States
2 March 2021, 02:03solution
Also, the classes are Cartoon and Thriller, not lowercase
+2
Karas Backend Developer
2 March 2021, 18:08
The getMovie method has to be called anyway on the wrong string also, per the requirements. Definitelly will not display anything but requires to be called. there it goes attempt number 102. Thanks.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
2 March 2021, 18:16
correct, but line 29 should only be reached for each valid entry.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym