GithubHelp home page GithubHelp logo

ucd-csci2312-pa1's Introduction

CSCI 2312: Programming Assignment 1

working with objects


C++ lets us program with objects. We describe objects in C++ by declaring and defining classes. We declare our class's structure in a header file, just like in C, and define it (that is, write the code that actually does the work) in a corresponding source code file.

Here is a sample header file Point.h that describes an object that represents a point in three-dimensional Euclidean space:

// A 3-dimensional point class!
// Coordinates are double-precision floating point.
class Point {

private:
    double x;
    double y;
    double z;

public:
    // Constructors
    Point();                      // default constructor
    Point(double x, double y, double z);    // three-argument constructor

    // Destructor
    ~Point();

    // Mutator methods
    void setX(double newX);
    void setY(double newY);
    void setZ(double newZ);

    // Accessor methods
    double getX() const;
    double getY() const;
    double getZ() const;
    
    double distanceTo(const Point & pass1, const Point & pass2) const;

};

We can instantiate, or create an instance of, our class anywhere in the rest of our code by calling any of the constructors we have defined:

Point myPoint;            // Calls Point::Point(). Notice the lack of parentheses!
Point myOtherPoint(5, 3, 4); // Calls two-argument constructor Point::Point(double, double, double). Notice auto type conversion!

Your Task

  1. Fork the Github repository for CSCI 2312 PA1. This becomes your remote repository, against which you will work. Then clone it to your local development environment (e.g. laptop). This becomes your local repository against which you will develop. You will find the Point.h file for the 2D Point class, along with a test suite and a driver file main.cpp.

  2. Change the Point class to represent points in three (3) dimensions. Make sure to update the comments to match this change. Implement the class in a source file Point.cpp.

  3. Add a new member function to Point called distanceTo. This member function should accept as an argument a const Point & (a reference to a constant Point), and it should return a double that approximates the distance between the two points. Note that distanceTo is a constant member function.

If you find a square-root function useful for this, the C standard library has one, called sqrt(). The function takes a double and returns another double.

To use it in your C++ code, you write

#include <cmath>

(This means, "Include the C math library header.") And then you are all set. Remember to always include your header files, for example,

#include "Point.h"

after all the Standard Library headers.

  1. Create a new source file area.cpp and implement the following function:
double computeArea(const Point &a, const Point &b, const Point &c);

which takes by reference three constant Point objects, computes the area within the triangle defined by these points (hint: use Heron's Formula), and returns it as a double-precision floating point number.

  1. If you comfortable with the command line, compile these sources together like so:
g++ -Wall main.cpp area.cpp Point.cpp PointTests.cpp ErrorContext.cpp -o ucd-csci2312-pa1

Note:

  • g++ is the GNU C++ compiler.
  • -Wall means "output ALL warnings." (If you see any warnings, be sure to fix them.)
  • Next comes the list of source files. (You don't list header files here. They are included by the source files.) main.ccp is the program driver file. Point.cpp is your implementation of the 3D Point. area.cpp is your implementation of the computeArea function. PointTests.cpp contains the implementation of the test suite. ErrorContext.cpp implements a class holding testing information and used in the test suite.
  • -o specifies the output file. In this case, we are making a program called ucd-csci2312-pa1.

If you are using CLion, you can just go to Run-->Build. The project already supplies a CMakeLists.txt file, which is read by the cross-platform build tool CMake. CLion uses CMake to create a makefile for your architecture. Here is a short tutorial on makefile and the C++ build process. CMake uses the directives in the CMakeLists.txt and works with the specific build tool for your platform to create the makefile. The file looks like this

cmake_minimum_required(VERSION 3.3)
project(ucd_csci2312_pa1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
     main.cpp
     area.cpp
     Point.cpp
     Point.h
     PointTests.h
     PointTests.cpp
     ErrorContext.cpp
     ErrorContext.h)

add_executable(ucd_csci2312_pa1 ${SOURCE_FILES})

For this project, all you need to do to run your Point code on CLion is to list all your *.cpp files as shown. Listing the headers as well doesn't hurt.

  1. Run the generated program:
./ucd_csci2312_pa1

In CLion, you first have to build (Run-->Build) and then run (Run-->Run 'ucd_csci2312_pa1') your project. Any output will appear in the built-in console.

  1. This assigment is based on a test suite. The test suite is written not against the original assignment but against the finished assignment. So the first time you run it, it won't even compile. For example, the test suite assumes a 3D Point whereas the original assignment has only a 2D Point.

Test-driven development (TDD) means that you write your test code before you write the actual code it has to test. You will be doing TDD, except that you already have the test suite. The test suite is written in such a way that earlier tests are more basic and later tests require earlier tests to be running. The most basic test is usually called a "smoke test", so it's best to start there. Here is a recommended strategy for completing PA1:

  • The test suite is in PointTests.h and PointTests.cpp. main.cpp runs the tests.
  • Comment out all but the first test in those files.
  • Read the error messages from the compiler and/or linker, and also the assignment requirements (this document).
  • Implement enough functionality to pass the first test.
  • Uncomment the next test from the driver and test suite files.
  • Repeat the procedure until all tests pass. At this time you are done with programming.
  • Change the comments where necessary.

Note: If you see that CMake is complaining that it can't find some of its files (e.g. area.cpp), you should create them. To add them to your git local repository, you need to execute the command

git add area.cpp

or, alternatively, in CLion, right-click on the new file, scroll down to Git and then click + Add.

  1. Commit all code changes to your local repository, then push the changes to the default master branch of your Github repository (aka remote repository).

  2. Update the README.md file in your remote repository with a description of your 3D Point class. (Clicking on a file takes you to a view where you can edit it.) You can use this file as a guide. Use the Github Markdown Cheatsheet as a reference.

  3. Coding style is important for your career. While this assignment is going to be graded strictly on passing the tests, subsequent assignments might also check for coding style. Here is a short C++ programming style guide you can use as a reference. Adopting a good style early on, when the assingment is small, will pay off greatly and very soon.

Grading

An autograding script will test your class on a variety of inputs and assign a grade based on the number of passed tests. (E.g. if your code passes 3 out of 6 test cases, your correctness score will be 50%). The test suite for PA1 has 56 tests.


ACKNOWLEDGEMENT: Modelled after CS11 Lab 1 at Caltech.

Some content Copyright (C) 2004-2010, California Institute of Technology.

ucd-csci2312-pa1's People

Contributors

ivogeorg avatar joseadanortega avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.