Object Oriented Programming with Java
ToledoSlackGitHub Org 1GitHub Org 2
Primary version
Primary version
  • About this Course
  • Introduction to Computer Programming
    • Introduction to Computer Programming
    • Challenges
    • Multiple Choice
  • Basic Building Blocks
    • Basic Building Blocks
    • Challenges
    • Multiple Choice
  • Starting in Java
    • Starting in Java
    • Challenges
    • Multiple Choice
  • Storing and Processing Data
    • Storing and Processing Data
    • Challenges
    • Multiple Choice
  • Making Decisions
    • Making Decisions
    • Challenges
    • Test Yourself
    • Multiple Choice
  • Loop Constructs
    • Loop Constructs
    • Challenges
    • Multiple Choice
  • Strings
    • Strings
    • Challenges
    • Multiple Choice
  • Arrays
    • Arrays
    • Challenges
    • Multiple Choice
  • Object Oriented Thinking
    • Object Oriented Thinking
  • All About Objects
    • All About Objects
    • Multiple Choice
  • Defining Classes
    • Defining Classes
    • Challenges
    • Multiple Choice
  • Methods
    • Methods
    • Challenges
    • Multiple Choice
  • Constructors
    • Constructors
    • Multiple Choice
  • Inheritance
    • Inheritance
  • Starting with JavaFX
    • Starting with JavaFX
  • Hands On
    • Hands on MQTT
    • Hands on GSON
  • Hack @ IT
    • Hack @ IT
    • Caesar Encryption
      • Solution
    • Complex Numbers
  • Assignments
    • Number Characteristics
    • Linear Equation
    • LineSegment
  • Videos
    • Videos
  • Sources
    • Sources
Powered by GitBook
On this page
  • A Parallelogram
  • Circle Properties
  • ArraySorter
  1. Methods

Challenges

PreviousMethodsNextMultiple Choice

Last updated 6 years ago

A Parallelogram

Create a class that stores the base and height of a parallelogram.

Add two methods to the class. One to determine the area and one to determine the circumference of the parallelogram.

Make a small main program to show that the implementation works.

Circle Properties

Write a Circle class that holds the radius of a circle. Also provide methods to calculate the area, circumference and diameter of the circle.

Make sure all the data types are double.

Next create a small main application that allows the user to input the radius of a circle. Output all the properties of that circle after the user inputted the radius.

HINT - Pi

You can create a variable to hold an approximation to the value of pi. However, Java can supply a more accurate version of PI. Can you find on the Internet how to access/use this value?

Once this all works alter the Circle class method setRadius() so it does not allow the user to provide a negative radius. A good option here might be to use the absolute value of that radius. So in other words, take the absolute value of the provided argument and assign that result to the attribute of Circle.

ArraySorter

Create a class called ArraySorter that can sort a pre-filled array of integers. Start from the code shown below for your main application.

public static void main(String[] args) {
  // Create array of 10 random numbers
  Random generator = new Random();
  int[] values = new int[10];
  for (int i = 0; i < values.length; i++) {
    values[i] = generator.nextInt(20);
  }

  System.out.println("Original array:");
  for (int i = 0; i < values.length; i++) {
    System.out.print(values[i] + " ");
  }

  // Choose sorting here
  ArraySorter sorter = new ArraySorter();
  sorter.selectionSort(values);

  System.out.println("\n\nSorted array:");
  for (int i = 0; i < values.length; i++) {
    System.out.print(values[i] + " ");
  }
}

Create a method called selectionSort() that takes in an array of integers. Sort the array in ascending order using the selection sort algorithm. Search the Internet for "selection sort". You will find enough information and examples. Do no copy / paste code, try to implement it for yourself.

The YouTube video Selection Sort | GeeksforGeeks gives a nice overview of the selection sorting algorithm:

Once this is operational, extend the class with a method called insertionSort() and implement the insertion sort algorithm. You can use the website for more information on insertion sort or again the YouTube video Insertion Sort | GeeksforGeeks which can be found at .

Want to have some nice background music while programming, than also checkout .

https://www.youtube.com/watch?v=xWBP4lzkoyM
https://www.geeksforgeeks.org/insertion-sort/
https://www.youtube.com/watch?v=OGzPmgsI-pQ
https://www.youtube.com/watch?v=kPRA0W1kECg
Parallelogram