Java Math and Constructor Methods in Action, Exams of Computer Science

This document showcases various java methods related to mathematics and constructor overloading using the mathutilities and pet/pet2 classes. Topics include static and non-static methods, method calls between static and non-static, math methods, constructor overloading, default constructor, and method overloading with automatic type casting and priority.

Typology: Exams

Pre 2010

Uploaded on 03/10/2009

koofers-user-4ov
koofers-user-4ov 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. static method
int k = MathUtilities.pow(3,4);
System.out.println(k);
//System.out.println(MathUtilities.PI);
//MathUtilities.PI=1;
2. static, non-static methods
MathUtilities math = new MathUtilities();
double k = math.area();
System.out.println(k);
3. call between static, non-static method
Try call static from non-static, nonstatic from static
4. Math methods
import java.lang.Math;
System.out.println("The max between 2 and 10 is
"+Math.max(2,10));
System.out.println("The abs of -100 is "+Math.abs(-
100));
System.out.println("The round of 10.4 is " +
Math.round(10.4));
System.out.println("The ceil of 10.4 is
"+Math.ceil(10.4));
System.out.println("The floor of 10.4 is
"+Math.floor(10.4));
pf3

Partial preview of the text

Download Java Math and Constructor Methods in Action and more Exams Computer Science in PDF only on Docsity!

  1. static method int k = MathUtilities.pow(3,4); System.out.println(k); //System.out.println(MathUtilities.PI); //MathUtilities.PI=1;
  2. static, non-static methods MathUtilities math = new MathUtilities(); double k = math.area(); System.out.println(k);
  3. call between static, non-static method Try call static from non-static, nonstatic from static
  4. Math methods import java.lang.Math; System.out.println("The max between 2 and 10 is "+Math.max(2,10)); System.out.println("The abs of -100 is "+Math.abs(- 100)); System.out.println("The round of 10.4 is " + Math.round(10.4)); System.out.println("The ceil of 10.4 is "+Math.ceil(10.4)); System.out.println("The floor of 10.4 is "+Math.floor(10.4));
  1. constructor overloading Pet dog = new Pet(); System.out.println("name:"+dog.name + " age:" + dog.age + " weight:" + dog.weight); Pet Cat = new Pet("Cat", 2, 3.0); System.out.println("name: "+Cat.name + " age:" + Cat.age + " weight:" + Cat.weight);
  2. default java constructor. call method in side method Pet2 Cat = new Pet2(); System.out.println("name: "+Cat.name + " age:" + Cat.age + " weight:" + Cat.weight); /////////////////////// Pet2 Cat = new Pet2("Cat", 2, 3.0); System.out.println("name: "+Cat.name + " age:" + Cat.age + " weight:" + Cat.weight); Cat.setPet("Small Cat", 1, 1.5); System.out.println("name: "+Cat.name + " age:" + Cat.age + " weight:" + Cat.weight);
  3. overloading, automatic type casting, priority double k = Statistician.average(2.0, 3.0); System.out.println(k); k = Statistician.average(2.0, 3.0, 4.0); System.out.println(k); int n = Statistician.average(2,3);