firstly, really sorry for my poor english.
i am trying to make a list of movies.
in main class i call the insert() method and in it i make an object of MovieListNode class in order to do what is needed.
class main{...
while( FileParsers.hasNextMovie() ){
MovieData movie = FileParsers.getNextMovie();
System.out.println( movie );
/* fill the movie lists here */
UnsortedMovieList vag=new UnsortedMovieList();
vag.insert(movie);
}
the insert method of unsortedmovielist:
class UnsortedMovieList{...
public void insert(MovieData data){
MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres());
if(isEmpty()){
tail=node;
}else{
head.setPrevious(node);
}
node.setNext(head);
head=node;
size++;
}
and the MovieListNode class(sorry for the size):
public class MovieListNode {
private int id;
private String title;
private int year;
private double rating;
private int votes;
private int duration;
private ArrayList<genre_t> genres;
private int i=0;
private MovieListNode previous;
private MovieListNode next;
public MovieListNode(){}
public MovieListNode(int id, String title, int year, double rating, int votes, int duration, ArrayList<genre_t> genres) {
this.id=id;
this.title=title;
this.year=year;
this.rating=rating;
this.votes=votes;
this.duration=duration;
this.genres=genres;
}
public int getId() {return id;}
public String getTitle() {return title;}
public int getYear() {return year;}
public double getRating() {return rating;}
public int getVotes() {return votes;}
public int getDuration() {return duration;}
public ArrayList<genre_t> getGenres() {return genres;}
public MovieListNode getPrevious() {return previous;}
public MovieListNode getNext() {return next;}
public void setNext(MovieListNode next) {this.next=next;}
public void setPrevious(MovieListNode previous) {this.previous=previous;}
}
when i do this i get NullPointerException in line MovieListNode node=new MovieListNode(data.getId(),data.getTitle(),data.getYear(),data.getRating(),data.getVotes(),data.getDuration(),data.getGenres()).instead if i write 'MovielistNode node=new MovielistNode();' i dont get any errors but it's not what i want.
if anyone could help i would be grateful. thanks. (if u want more information about something in my code please let me know)
One or all of the fields in your MovieData object are null. You need to investigate your method:
FileParsers.getNextMovie();
You use this method to initialize an object of type MovieData If this method does not declare and initialize a MoveData object with a constructor that initializes all of the data fields, and then return that object, you will get a NullPointer when you try to call one of the getters
It is probably genres since the other fields have default initializations.
UPDATE:
In your Fileparsers class. I notice the code in the getNextMovie() method:
if( dataLine==null || genresLine==null )
return null;
}
It may be that your logic to readLine() is not being used correctly. So I suspect you may be ending up with null lines and therefore returning a null MovieData object. You should check if the next line is null before assigning it.
Related
I made an class called myLinkedList that stores nodes from a class called LinkNodes that takes an object with an name(String), as a field. I want to sort the nodes in my list alphabetical, from the memberPlayer field firstName
public class LinkNode {
public memberPlayer player;
public LinkNode next;
public LinkNode() {
this(null, null);
}
public LinkNode(memberPlayer player) {
this(player,null);
}
public LinkNode(memberPlayer player, LinkNode next) {
this.player = player;
this.next = next;
}
public String toString() {
String result = player + " ";
if (next != null) {
result += next.toString();
}
return result;
}
}
I have tried with the collection.sort method but, without luck, as i tried to use it on an list that i created myself, but it worked fine, when i just used the objects. Is there somehow special i need to do, if I want to acces the field of an object inside a node?
memberPlayer class:
public class memberPlayer implements Comparable<memberPlayer>{
private String firstName;
private String lastName;
private int age;
private String team;
}
You should implement the compareTo method in the Comparable interface to use a specific field.
#Override
public int compareTo(Object o) {
MemberPlayer player = (MemberPlayer)o;
return this.firstName.compareTo(player.firstName);
}
P:S Always use proper conventions when naming classes.
I have been creating a java text game but I am stuck trying to figure out on how to implement the last 2 methods. I want it to print out the items in the room and the name of the npc thats in the room (A sort of a Look function). I am not sure on how to go on about it. Any help would be appriciated.
Room[] place = new Room[]{station, UC, Ollies, lounge, palace, AT301};
Sword sword = new Sword();
Thing heal = new HealthPotion();
Thing armour = new Armour();
Thing trap = new Trap();
and for the NPC (Mike, Jake, Evil, Carl)
public abstract class Player{
//abstract attributes
private String name;
private int currentHealth;
private int maxHealth;
private int damage;
private Room currentRoom;
private int stack;
private int effect;
//Constructor for player
public Player(String name, int currentHealth, int maxHealth, int damage, int effect, int stack){
this.name = name;
this.currentHealth = currentHealth;
this.maxHealth = maxHealth;
this.damage = damage;
this.effect = effect;
this.stack = stack;
}
//getters
public String getName(){ return name;}
public int getCurrentHealth(){ return currentHealth;}
public int getMaxHealth(){ return maxHealth;}
public int getDamage(){ return damage;}
public Room getCurrentRoom(){ return currentRoom;}
public int getEffect(){ return effect;}
public int getStack(){ return stack;}
//setters
public void setCurrentHealth(int currentHealth){this.currentHealth = currentHealth;}
public void setMaxHealth(int maxHealth){this.maxHealth = maxHealth;}
public void setDamage(int damage){ this.damage = damage;}
public void setCurrentRoom(Room room){this.currentRoom = room;}
public void setEffect(int effect){ this.effect = effect;}
public void setStack(int stack){ this.stack = stack;}
public void enter(Room room){ this.currentRoom = room;}
//abstract method because each player has a different attack;
public void takeDamage(int damage){ setCurrentHealth(this.currentHealth-damage);}
public boolean isDead(){
if(this.currentHealth<=0){ return true;}
return false;
}
}
I was able to make everything functional except the Look function for the player. I can't figure out how to go on about it.
Room is a vector of items right ? If so you can do a function on the player class that when it is called it goes to the vector of the room you're in and simply print out the items that are in the vector, something like this:
String lookAround(){
ArrayList temp = (ArrayList)getCurrentRoom(); //returns the array containing the items in the current room
for(Thing i : temp){
i.getDescription(); //Method present in all classes that come from Thing that prints out the name of the item and/or its caracheteristics
}
}
In the array of the room you should try to include the name of all players in the room including yourself so that you can print out everyone present in the room
Hope this helps
I would recommend you first of all creating a class Npc with attribute name and add it to the room.
this class works:
public class Undergrad extends Student{
private int year;
private int numOfCourses=0;
private Course[] courses=new Course[4];
public Undergrad(int year,String name,String major,double gpa){
super(name,major,gpa);
this.year=year;
}
public int getYear(){
return year;
}
public void setYear(int year){
this.year=year;
}
public String getName(){
return name;
}
public String getMajor(){
return major;
}
public double getGpa(){
return gpa;
}
public void setGpa(double gpa){
this.gpa=gpa;
}
public Course[] getCourses(){
return courses;
}
public void addCourses(Course course){
if(numOfCourses>=4){
System.out.println("Student can not study");
} else{
courses[numOfCourses]=course;
numOfCourses++;
}
}
public void printCourses(){
for(int i=0;i<numOfCourses;i++){
System.out.println(courses[i].toString());
}
}
public String toString(){
return "Student name"+name+"courses"+numOfCourses;
}
}
but I was just wondering shouldn't "course" have an "s" so it becomes courses, so it can become the same as the name in the array "courses". Also is the printCourse method the same as the System.out.print method?
public void addCourses(Course course)
Course is simply the name of the class. Even if there are plenty of Course objects, the name stays the same.
As of courses is the name of the object's reference so you're free to choose it while creating it.
If your question is why in addCourses(Course course) course does not have an s, it is because it is a reference to a Course object and not to the array.
courses[numOfCourses]=course
Here, we're adding course to the courses array of Course objects.
To your question, it doesn't really make any difference. Your 'addCourses' method is not a setter or getter method, so should in future you want to use the class for any purposes that imply serialization you will be fine.
But just for readability you could rename it to void addCourse(Course c).
In addition, you could guard against potential null argument using the Optional class added in Java 8.
public void addCourse(Course c){
if(numOfCourses>=4){
System.out.println("Student can not study");
} else{
courses[numOfCourses]=Optional.ofNullable(c).orElse(new Course(argument 1, argument 2, argument etc));
numOfCourses++;
}
}
I think it's normal desire to have access for manipulating with data. Should i create getters/setters in Record class, if i already use Storage for this goal, made on Record? Or what need to do?
public class Storage {
List<Record> record;
public Storage(){
this.record = new ArrayList<Record>();
}
public void addRecord(Record record) {
this.record.add(record);
}
public Record getRecord(int number){
return this.record.get(number);
}
public class Record {
private int number;
private int count;
private Object code;
/* public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
} */
public Record(int number, int count, Object code) {
this.number = number;
this.count = count;
this.code = code;
}
#Override
public String toString() {
return (this.number+" "+this.code+" "+ this.count);
}
}
According to me it is not require to write setter and getter methods until you don't want to modify them.In your code you have initialized fields in constructor and overridden toString() method to get meaningful content of Record object and never trying to modify Record 's fields directly.
I strongly feel that it is not required.
I'm trying to make my own tree class but keep getting horribly confused. Basically I'm trying to make a weighted tree. I've made the following node class:
import java.util.*;
public class subdivNode {
private int nodevalue;
private int nodeID;
private List<subdivNode> childnodes;
public subdivNode(int value, int id){
nodevalue = value;
nodeID = id;
}
public int getValue(){
return nodevalue;
}
public int getId(){
return nodeID;
}
public void addChild(subdivNode child){
childnodes.add(child);
}
public int getNumChildren(){
return childnodes.size();
}
public subdivNode getChild(int pos){ //return's i'th child
return childnodes.get(pos);
}
}
And this is the skeleton for my tree class so far:
public class subdivTree {
private subdivNode rootnode;
public subdivTree(){
rootnode = new subdivNode(0,0);
}
public void addNode(int parent, int value){
}
public int getNodeValue(int node){
return 0;
}
public int getNumChildren(int node){
return 0;
}
}
Beyond that I have no idea
EDIT: Sorry for the ambiguity. My question should have been how would I go about implementing the addnode method in subdivTree. The end goal is to create an alogrithim which searches the tree for the path between any two nodes such that the greatest value(adding the value of all the in between nodes) is obtained.
Before working on addNode, tell us how getNodeValue(int node) is going to work.
You have a rootNode, what method of that are you going call with that "node" value?
I'm not sure, but I think that your interface is broken. I think your concepts of get by position and get by Id are confused.
Draw a picture of the data structure you expect.