CMPG 211 1-1 V 2021     Expand All Next Week 3Practical 2: Methods Main page content           Methods - Week 3                 Overview   A method is a collection of statements grouped together to perform an operation. You have used some predefined methods in Java like System.exit, Math.pow, and Math.random.         Resources   Textbook: Laing, Chapter 6               Understand Java   Read Par 6.1 on the purpose and example of methods Read Par 6.2 on Defining/writing a method (pp. 204 - 206)   Read Par 6.3 on How to call a method (pp. 206 - 208)                 Practical 2: Methods Task 1Click to collapse           Task 1   See the given Java source code below to enter the radius of a circle and calculate and display the area and circumference of the circle: import java.util.Scanner; public class CalcCircleMethods {     public static void main(String [] args)     {         Scanner input = new Scanner(System.in);                  System.out.println("Enter the radius of a circle");         double radius = input.nextDouble();                  double circumference = 2 * Math.PI * radius;         double area = Math.PI * Math.pow(radius, 2);                  System.out.println("The radius is " + radius);                  System.out.printf("%s%1.1f\n", "The circumference is ", circumference);         System.out.printf("%s%1.2f\n", "The area is ", area);     } } Create a new Java project called CircleProject. (1.1) Create a class called staticMethods.  Use the same code to calculate and display the area and circumference of a circle BUT the class must calculate and return the area and circumference of a circle using two static methods called calcArea() and calcCircumference().  The methods must receive the radius as parameter. The methods must be called from the main method. Hint: Call/invoke the methods from the main method as follows: area = calcArea(radius); circumference = calcCircumference(radius); (1.2) Object-oriented code: Create a class called testCircle and an object class called Circle. The object class Circle must contain two methods calcArea() and calcCircumference(), NOT the test class. The testCircle class must contain ONLY the main method.  The main method must ask the user to enter the radius and call the methods from the object class to calculate and return the area and circumference of the circle.  The main method must display the are and circumference of the circle. Hint: A Circle object must be instantiated in the test class to be able to use the methods of the object class.  Use the following statement to instantiate a Circle object: Circle myCircle = new Circle(); The name of the object that you have created/instantiated is myCircle. Call the methods as follows: area = myCircle.calcArea(radius);   Try to complete the tasks on your own, before watching the videos below.       Task 1.1 Video           Task 1.2 Video                   Understand Java   Read Par 6.4 on void method examples (pp. 209 - 210) Answer Questions 6.1 - 6.10 (p. 211) Read Par 6.5 on Passing arguments (p. 212) Answer Questions 6.11 - 6.14 (p. 214) Read Par 6.6 on Modularizing code (p. 215) Study the two examples Read Par 6.7 Case study (p. 217) Read Par 6.8 on Overloading Methods (p. 219) Overloading methods - methods with the same name with different parameters and return types/signatures. Signature is the header of a method Example on p. 220 (Three methods with the name max) Answer Questions 6.15 - 6.17 (pp. 221 - 222) Task 2Click to collapse           Task 2   Write a Java program to test for palindrome integers and strings. A number/string is a palindrome if its reversal is the same as itself. In the object class Palindrome, write the following methods: public int reverse(int number) - returns the reversal of an integer, i.e., reverse(456) will return 654 public boolean isPalindrome(int number) - return true if the number received as parameter is a palindrome.  Use the reverse method to implement isPalindrome. Write a testPalindrome class that prompts the user to enter an integer and reports whether the integer is a palindrome. Example of output for input 1221: The number 1221 is a palindrome

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
CMPG 211 1-1 V 2021

 

 
Expand All Next
Week 3Practical 2: Methods

Main page content

 

 
 
 

 

Methods - Week 3

 

 

 
 
 
     

Overview

 
  • A method is a collection of statements grouped together to perform an operation. You have used some predefined methods in Java like System.exit, Math.pow, and Math.random.

 
     

Resources

 

Textbook: Laing, Chapter 6

 

 
 
 
     

Understand Java

 
  • Read Par 6.1 on the purpose and example of methods
  • Read Par 6.2 on Defining/writing a method (pp. 204 - 206)

 

  • Read Par 6.3 on How to call a method (pp. 206 - 208)

 

 

 
 
 
     

Practical 2: Methods

Task 1Click to collapse

 
 
 
   

Task 1

 

See the given Java source code below to enter the radius of a circle and calculate and display the area and circumference of the circle:

import java.util.Scanner;
public class CalcCircleMethods
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter the radius of a circle");
        double radius = input.nextDouble();
        
        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * Math.pow(radius, 2);
        
        System.out.println("The radius is " + radius);
        
        System.out.printf("%s%1.1f\n", "The circumference is ", circumference);
        System.out.printf("%s%1.2f\n", "The area is ", area);
    }
}

Create a new Java project called CircleProject.

  • (1.1) Create a class called staticMethods.  Use the same code to calculate and display the area and circumference of a circle BUT the class must calculate and return the area and circumference of a circle using two static methods called calcArea() and calcCircumference().  The methods must receive the radius as parameter.

The methods must be called from the main method.

Hint: Call/invoke the methods from the main method as follows:

area = calcArea(radius);

circumference = calcCircumference(radius);

  • (1.2) Object-oriented code:

Create a class called testCircle and an object class called Circle.

The object class Circle must contain two methods calcArea() and calcCircumference(), NOT the test class.

The testCircle class must contain ONLY the main method.  The main method must ask the user to enter the radius and call the methods from the object class to calculate and return the area and circumference of the circle.  The main method must display the are and circumference of the circle.

Hint:

Circle object must be instantiated in the test class to be able to use the methods of the object class.  Use the following statement to instantiate a Circle object:

Circle myCircle = new Circle();

The name of the object that you have created/instantiated is myCircle.

Call the methods as follows:

area = myCircle.calcArea(radius);

 
  • Try to complete the tasks on your own, before watching the videos below.

 
   

Task 1.1 Video

 

 

 
   

Task 1.2 Video

 

 

 

 
 
 
     

Understand Java

 
  • Read Par 6.4 on void method examples (pp. 209 - 210)
    • Answer Questions 6.1 - 6.10 (p. 211)
  • Read Par 6.5 on Passing arguments (p. 212)
    • Answer Questions 6.11 - 6.14 (p. 214)
  • Read Par 6.6 on Modularizing code (p. 215)
    • Study the two examples
  • Read Par 6.7 Case study (p. 217)
  • Read Par 6.8 on Overloading Methods (p. 219)
    • Overloading methods - methods with the same name with different parameters and return types/signatures.
    • Signature is the header of a method
    • Example on p. 220 (Three methods with the name max)
    • Answer Questions 6.15 - 6.17 (pp. 221 - 222)

Task 2Click to collapse

 
 
 
   

Task 2

 

Write a Java program to test for palindrome integers and strings.

A number/string is a palindrome if its reversal is the same as itself.

In the object class Palindrome, write the following methods:

  • public int reverse(int number) - returns the reversal of an integer, i.e., reverse(456) will return 654
  • public boolean isPalindrome(int number) - return true if the number received as parameter is a palindrome.  Use the reverse method to implement isPalindrome.

Write a testPalindrome class that prompts the user to enter an integer and reports whether the integer is a palindrome.

Example of output for input 1221:

The number 1221 is a palindrome

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Calling
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education