Locate the errors in the following code

Assignment Help JAVA Programming
Reference no: EM13842158

Part A
· Locate the errors in the following code. You will notice, not every line will contain a mistake. Pay close attention to the line preceding it. Look out for uninitialized pointers, NULL pointers, pointers to deleted objects, and confusing pointers with objects.

1 int* p = new int;

2 p = 5;

3 *p = *p + 5;

4 Employee e1 = new Employee("Hacker, Harry", 34000);

5 Employee e2;

6 e2->set_salary(38000);

7 delete e2;

8 Time* pnow = new time ();

9 Time* t1 = new Time(2, 0, 0);

10 cout << t1->seconds_from(pnow);

11 delete*t1;

12 cout << t1->get_seconds();

13 Employee* e3 = new Employee("Lin, Lisa", 68000);

14 cout << e3.get_salary();

15 Time* t2 = new Time(1, 25, 0);

16 cout << *t2.get_minutes();

17 delete t2;


Part B

· Implement a class person with the following fields:

The name

A pointer to the persons best friend

A popularity counter that indicates how many people have this person as their best friend

· Write a program that reads in a list of names, allocates a new Peron for every one of them, and places them in a vector<Person*>. Next, request the name of each best friend for every Person objects. Find the object matching the friend's name, and call a set_best_friend member function to update the pointer and counter. Lastly, print out every Person objects. Labeling the name, best friend, and popularity counter for all.

#include <iostream>

#include <string>

#include <vector>

#include <iomanip>

using namespace std;

class Person

{

public:

Person(string name = "N/A", int popularity = 0, Person* bfriend = NULL);

string getName();

void setName(string name);

int getPopularity();

void setPopularity(int pop);

string getBestFriend();

void setBestFriend(Person* bfriend);

private:

string name; //The name

Person* bestfriend; //A pointer to the persons best friend

int popularity; //A popularity counter that indicates how many people have this person as their best friend

};

Person::Person(string fname, int fpopularity, Person* bfriend)

{

name = fname;

popularity = fpopularity;

bestfriend = bfriend;

}

string Person::getName()

{

return name;

}

void Person::setName(string fname)

{

name = fname;

}

int Person::getPopularity()

{

return popularity;

}

void Person::setPopularity(int pop)

{

popularity = pop;

}

string Person::getBestFriend()

{

return bestfriend->getName();

}

void Person::setBestFriend(Person* bfriend)

{

bestfriend = bfriend;

}


/*• Write a program that reads in a list of names, allocates a new Peron for every one of them,and places them in a vector<Person*>. Next, request the name of each best friend for everyPerson objects. Find the object matching the friend's name, and call a set_best_friend memberfunction to update the pointer and counter. Lastly, print out every Person objects.Labeling the name, best friend, and popularity counter for all.*/

Reference no: EM13842158

Questions Cloud

Implement a base class person : Implement a base class person
Payoffs always represent profits in decision analysis : Payoffs always represent profits in decision analysis problems. As the name suggests, the maximum likelihood criterion says to focus on the largest payoffs. Using Bayes' decision rule will always lead to larger payoffs.
Create database objects to track the construction projects : You will use a database template to enter data into the Events table. You will create a simple query, a form, and a report.
The different theories of intelligence proposed : Provide a summary of the different theories of intelligence proposed in the textbook (Spearman, Sternberg, Gardner, and Salovey/Mayer's theories);Describe which theory you believe best describes intelligence;explain why you feel this way;Describe whe..
Locate the errors in the following code : Locate the errors in the following code
Write a menu-driven java program : Write a menu-driven JAVA program that performs the following array/vector operations:
How does a date escalate to rape : How does a date escalate to rape?  There has been a drastic increase in the use of date rape drugs over the last several years.  Give some examples of current drugs being used, how they are administered, and their effects on the victim. Each students..
Value chain-competitive advantage and customer delight : Please provide a one paragraph summary related to reviewing the following three critical concepts (value chain/competitive advantage/customer delight) when thinking about how a value chain creates competitive advantage.
What was the net or cash cost for the merchandise : Longoria Company purchased merchandise inventory on account with a list price of $20,000 and credit terms of 3/10, n/30. What was the net or cash cost for the merchandise?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Descriptionyou are to write a program that determines the

descriptionyou are to write a program that determines the day of the week for new years day in the year 3000. to do

  Write a java program that prints the numbers from 1 to 125

Write a Java Program that prints the numbers from 1 to 125. But for multiles of three print "Batman" instead of the number and for the multiples of five print "Superman". For instances which are multiples of both three and five "JusticeLeague".

  Calculates the total annual compensation of a salesperson

Write a Java application using an Integrated Development Environment (IDE) that calculates the total annual compensation of a salesperson

  Three most primary forms of loops: the while loop, the do..

explored the world of loops we have accepted the three most primary forms of loops: the while loop, the do...while loop (even though Python does not use this loop), and the for loop. The for loop has approximately three primary implementations that w..

  Java''s type int has limit onhow large aninteger it can store

Java's type int has a limit on how large an integer it can store. This limit can be circumvented by representing an integer as an array of digits. Write an interactive program that adds two integers of up to 50 digits each.

  Implement a thread for each creature representing a task

Implement a threads and a GUI interface using advanced Java Swing classes.

  What extent do images and themes of daughterhood

What extent do images and themes of daughterhood from your 19th century text influence each other?

  Prepare a mobile android app

The app should target Android and be written in Java, unless you have obtained prior permission from your instructor to target a different platform

  Define a simple interface named motorized with one method

question 1 define a simple interface named motorized with one method maxspeed which returns the maximum attainable

  Modify the book class to accommodate multiple authors

modify the Book class to accommodate multiple authors using one of the components from the Java Collection Framework.

  Write a java program that asks the user for his/her gender

According to researchers at Stanford Medical School, the ideal weight for a woman is found by multiplying her height in inches by 3.5 and subtracting 108.

  String that contains at least five letters

Write an application that prompts the user for a String that contains at least five letters and at least five digits. Continuously reprompt the user until a valid String is entered. Display a message indicating whether the user was successful or did ..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd