Question

This project involves writing a program with functionality of your choosing. You must include the programming...

This project involves writing a program with functionality of your choosing. You must include the programming elements as described below and must adhere to the general requirements below.

Programming elements: The code must contain the following elements:


(You may complete one program containing all of these elements or you may submit more than one program where all program together contain the following elements.)

Classes (at least 3)
Instance Fields and Methods
Constructors
Overloaded Method/Constructor (at least one)
Arrays / ArrayLists (at least one)
Parameter passing (at least one object passed as an argument)
Use of the toString method (for one of your objects)
Use of the equals method (for one of your objects)
Use of two Character Class methods (isDigit, isLetter, isLetterorDigit, is LowerCase, isUpperCase, is SpaceChar, toLowerCase, toUpperCase, …)
Use of two String methods (indexOf, lastIndexOf, getChars, toCharArray, Concat, replace, trim, …)
Inheritance (at least one use)
Exception Handling (at least one try-catch block)
Use of GUI Components (at least two: buttons, labels, text fields, checkboxes, radio buttons, combo boxes, sliders)
Note: You may complete one program containing all of these elements or you may submit more than one program where all program together contain the following elements.


General requirements:

No compile or runtime errors
Code is commented and documented (functionality of entire program is adequately described as well as methods are adequately described)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//program  1
import java.util.ArrayList;

class Shape2D {
    //instance fields
    int len, width;

    //constructor
    Shape2D() {
        len = width = 0;
    }

    //constructor overloading
    public Shape2D(int len, int width) {
        this.len = len;
        this.width = width;
    }

    //instance methods
    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    void printArea() {
        System.out.println("Area = " + this.len * this.width);
    }

}

//class Shape3D inherits from class Shape2D
class Shape3D extends Shape2D {
    //instance fields
    int height;

    {
        //this blocks gets called before the constructor
        System.out.println("\nInitialising instance variables with default values...");
        this.len = 0;
        this.width = 0;
        this.height = 0;
        System.out.println("len = " + len + " wid = " + width + " height = " + height);
    }

    //constructor
    public Shape3D() {
    }

    //constructor overloading
    public Shape3D(int len, int width, int height) {
        System.out.println("Constructor called.");
        this.len = len;
        this.width = width;
        this.height = height;
        System.out.println("len = " + len + " wid = " + width + " height = " + height);
    }

    //constructor overloading
    public Shape3D(int a) {
        System.out.println("Constructor called.");
        this.len = a;
        this.width = a;
        this.height = a;
        System.out.println("len = " + len + " wid = " + width + " height = " + height);
    }

    //instance methods
    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    void printVol() {
        System.out.println("Volume = " + this.len * this.width * this.height);
    }

}


class MyJavaTutorial {

    //this method can throw ArithmeticException so function call must be surrounded with try catch block
    private static int divideAbyB(int a, int b) throws ArithmeticException {
        return a / b;
    }

    private static void checkIfStringsAreEqual(String s1, String s2) {
        //using the equals method
        if (s1.equals(s2)) {
            System.out.println("Both the strings are matching.");
        } else {
            System.out.println("The strings are not matching.");
        }
    }

    //this method uses Character class methods
    private static boolean isStringLowerCase(String str) {

        //converts the String to char array
        char[] charArray = str.toCharArray();

        for (int i = 0; i < charArray.length; i++) {

            //checks if the character is a letter
            if (Character.isLetter(charArray[i])) {

                //if any character is not in lower case, return false
                if (!Character.isLowerCase(charArray[i]))
                    return false;
            }
        }

        return true;

    }

    //takes an object of Shape3D class as parameter
    private static void printCuboidOrCube(Shape3D shape3D) {
        if (shape3D.getWidth() == shape3D.getLen() && shape3D.getLen() == shape3D.getHeight()) {
            System.out.println("\nIt\'s a cube.");
        } else {
            System.out.println("\nIt\'s a cuboid.");
        }
    }

    public static void main(String[] args) {

        Shape3D cuboid = new Shape3D(4, 5, 6);
        Shape3D cube = new Shape3D(4);

        //creating an arraylist of  Shape3D objects
        ArrayList<Shape3D> shape3DArrayList = new ArrayList<>();
        shape3DArrayList.add(cuboid);
        shape3DArrayList.add(cube);

        for (Shape3D shape3D : shape3DArrayList) {
            printCuboidOrCube(shape3D);
            shape3D.printArea();
            shape3D.printVol();
        }
        System.out.println();

        String s1 = "Pet";
        String s2 = " is sleeping";
        String s3 = "Pet";
        checkIfStringsAreEqual(s1, s2);
        checkIfStringsAreEqual(s1, s3);
        System.out.println();

        //using String class methods...
        System.out.println(s1.indexOf('e') + "\n");    //prints the index of 'e' in string s1
        String s4 = "My pet dog Tommy";
        s4 = s4.concat(s2);     //using concatenation method
        System.out.println(s4 + "\n");
        s3 = s3.toLowerCase();  //converting the string to lower case
        System.out.println(s3);

        if (isStringLowerCase(s2)) {
            System.out.println("\nString \'" + s2 + "\' is in lower case.");
        } else {
            System.out.println("\nString \'" + s2 + "\' is not in lower case.");
        }


        System.out.println();
        //Exception Handling
        int a = 8;
        int b = 0;
        try {
            int c = divideAbyB(a, b);
            System.out.println(c);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

}
//output screenshot for this program : 

//program 2 using gui components

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class MyGui{
    public static void main(String args[]){

        JFrame frame = new JFrame("My frame");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //labels
        Label l1 = new Label("First :");
        Label l2 = new Label("Second :");

        //TextFields
        TextField t1 = new TextField(10);
        TextField t2 = new TextField(10);

        //Button
        Button button = new Button("OK");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //swapping the contents of the two textfields on click of the button
                String text1 = t1.getText().trim();
                String text2 = t2.getText().trim();
                t1.setText(text2);
                t2.setText(text1);
            }
        });

        //adding the components to the frame
        frame.add(l1);
        frame.add(t1);
        frame.add(l2);
        frame.add(t2);
        frame.add(button);

        frame.setLayout(new FlowLayout());
        frame.setSize(350, 350);
        frame.setVisible(true);
    }
}

//output of program 2

Add a comment
Know the answer?
Add Answer to:
This project involves writing a program with functionality of your choosing. You must include the programming...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from a data file. This program consists of two parts. The first part is the Weight class and the second part is the Program Core. The Weight Class is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods...

  • Please help with this assignment For this assignment, you must design and develop a program to...

    Please help with this assignment For this assignment, you must design and develop a program to read any CSV file and output an HTML table. The program should be able to input any csv file with any number of fields and use the field headings in the CSV file's first row to become the table headings of the table. The program should output an HTML Bootstrap table with table headings and alternating zebra strips for rows. Your program should demonstrate...

  • Please help with this assignment For this assignment, you must design and develop a program to...

    Please help with this assignment For this assignment, you must design and develop a program to read any CSV file and output an HTML table. The program should be able to input any csv file with any number of fields and use the field headings in the CSV file's first row to become the table headings of the table. The program should output an HTML Bootstrap table with table headings and alternating zebra strips for rows. Your program should demonstrate...

  • Programming Assignment 9 The purpose of this programming project is to demonstrate a significant culmination of...

    Programming Assignment 9 The purpose of this programming project is to demonstrate a significant culmination of most constructs learned thus far in the course. This includes Lists, Classes, accessors, mutators, constructors, implementation of Comparable, Comparator, use of Collections sort, iterators, properly accessing fields of complex objects, and fundamental File I/O. BACKGROUND Look over Programming Project #4 at the end of the Searching & Sorting Chapter (13), page 869. Programming Assignment 9 is similar with some added features as described below....

  • [python3] Start by using UML and planning the functionality of each method (including the constructor, that...

    [python3] Start by using UML and planning the functionality of each method (including the constructor, that is, the __init__() method) Initialization of your objects must require at least one parameter (in addition to self) All of your objects attributes/fields should be 'private' and you should include getters and setters for each attribute/field You must be able to print your object in a meaningful way and you should define what it means for two objects to be equal Additionally, your object...

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

  • 1/3 2/3 3/3 This programming assignment involves adding functionality to the SearchTreeset implementation found in the...

    1/3 2/3 3/3 This programming assignment involves adding functionality to the SearchTreeset implementation found in the SetMap project. Specifically you will edit the file util.SearchTreeSet and write Java-correct implementations of these methods * first pollFirst headset e lower The goal is to make these methods behave exactly like they would for a Java Treeset. Additional programming requirements the first, pollFirst, and lower methods should just take one or two passes down the tree. For a "well-balanced" tree, the time should...

  • When answering this question, can you please specify what you name your files? Thank you! Write a...

    When answering this question, can you please specify what you name your files? Thank you! Write a Java application, and an additional class to represent some real-world entity. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: a. Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT