{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "toc: true\n", "comments: true\n", "layout: notebook\n", "title: Classes\n", "description: The difference in the application and use of Value and Reference types in Java.\n", "authors: Kevin Du\n", "courses: { compsci: {week: 1} }\n", "type: hacks\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. **Class Instantiation**:\n", " - **Purpose**: Creating an object (instance) of a class.\n", " - **Syntax**: `ClassName objectName = new ClassName();`\n", " - **Explanation**:\n", " - `ClassName` refers to the name of the class you want to instantiate.\n", " - `objectName` is the reference variable that points to the newly created object.\n", " - `new ClassName()` allocates memory and initializes the object.\n", "\n", "2. **Constructors**:\n", " - **Purpose**: Special methods used for object initialization.\n", " - **Default Constructor**:\n", " - Takes no arguments.\n", " - Initializes instance variables to default values (e.g., `0`, `null`, etc.).\n", " - **Parameterized Constructors**:\n", " - Accept one or more arguments.\n", " - Used to set initial values for instance variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "public class MyClass {\n", " private int value;\n", "\n", " // Parameterized constructor\n", " public MyClass(int initialValue) {\n", " value = initialValue;\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instantiation of Classes\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "public class WordPair { \n", " /** Constructs a WordPair object. */ \n", " public WordPair(String first, String second) \n", " { \n", " /* implementation not shown */ \n", " } \n", " /** Returns the first string of this WordPair object. */ \n", " public String getFirst() \n", " { \n", " /* implementation not shown */ \n", " } /** Returns the second string of this WordPair object. */ \n", " public String getSecond() \n", " {\n", " /* implementation not shown */ \n", " } \n", "}\n", "public class WordPairList { \n", " /** The list of word pairs, initialized by the constructor. */ \n", " private ArrayList allPairs; \n", " /** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */ \n", " public WordPairList(String[] words) \n", " { \n", " /* to be implemented in part (a) */ \n", " } \n", " /** Returns the number of matches as described in part (b). */ \n", " public int numMatches() { \n", " /* to be implemented in part (b) */\n", " } \n", "}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here are the results for example 2 and 3\n", "0\n", "1\n", "2\n" ] } ], "source": [ "public class WordPair {\n", " private String First;\n", " private String Second;\n", " /** Constructs a WordPair object. */ \n", " public WordPair(String first, String second) \n", " { \n", " this.First = first;\n", " this.Second = second;\n", " } \n", " /** Returns the first string of this WordPair object. */ \n", " public String getFirst() \n", " { \n", " return this.First;\n", " } /** Returns the second string of this WordPair object. */ \n", " public String getSecond() \n", " {\n", " return this.Second;\n", " } \n", "}\n", "public class WordPairList { \n", " /** The list of word pairs, initialized by the constructor. */ \n", " private ArrayList allPairs; \n", " /** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */ \n", " public WordPairList(String[] words) \n", " { \n", " this.allPairs = new ArrayList(); //Some people miss this for some reason. You need an array list so that way you can add more pairs needed in the future.\n", " for (int i = 0; i allPairs; \n", " /** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */ \n", " public WordPairList(String[] words) \n", " { \n", " this.allPairs = new ArrayList(); \n", " for (int i = 0; i(); \n", " for (int i = 0; i