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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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