CSE 14 Lecture Notes - Lecture 4: Power2, Power1

36 views8 pages
21 May 2018
School
Course
Professor
CMPS12A Lecture 4 Classes, Methods, and Conversions of Types
In Java programming, classes and methods are essential in order to make a good program. It
akes the pogaes life easie  ipleetig these ethods, futios, o lasses ito
the code, minimizing the amount of work that you need to do. It is why for most occasions, you
can just import these classes, just like Math in a way like:
import java.lang.Math;
This lets the programmer be able to use the Math class functions like:
Math.sqrt()
Math.abs()
Math.round()
Math.pow()
etc.
One example of code when having to do more work to do the same thing:
Example:
class Example1 {
public static void main(String [] args) {
int x = 6, y = 10;
int z;
z = (x*x*x*x) + y;
System.out.println(z);
}
}
Another example using the imported Math class to do the same thing:
Example:
class Example2 {
public static void main(String [] args) {
int x = 6, y = 10;
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
int z;
z = Math.pow(x, 4) + y;
System.out.println(z);
}
}
As you can see, using the method from the Math class makes your life easier and makes code
more readable. In this example, the variable x is multiplied with itself 4 times which is also the
same thing as saying x to the power of 4. That is where the pow() method comes in, where you
can just state the variable x and by what power you want it to be.
For big programs, it would be best to make a function that does this so you will be able to use
such a method for anything you want instead of calling Math.pow(<base>, <exponent>). One
example to do this would be:
Example:
class Example3 {
public static void main(String [] args) {
int x = 6, y = 10, z, a;
z = power1(x); //this is a function that only takes the power of itself
a = power2(y, x); /
System.out.println(The output fo futio powe is:  + z);
“ste.out.pitlThe output fo futio powe is:  + a;
}
static int power1(int b) {
int c;
c = b * b;
return c;
}
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
static int power2(int a, int b) {
int c;
c = Math.pow(a, b);
return c;
}
This shows two functions that I made that takes the power of what was put in the argument
list. the function power1() takes the power of itself in the argument list and returns it while
power2() takes two variables in the argument list and takes var a to be the power of what is in
var b and returns it. You will learn more about functions later and how they work, but I am
trying to show how making functions of your own can be beneficial.
There is a method of String called valueOf() which turns anything that is in the argument list ()
into a string itself.
Example:
int x = 6500;
ha  = p;
String number = String.valueOf(x); //ue ow euals  which is not integer 6500
String character = String.valueOf(c); //haate ow euals p whih is ot haate p
To recap the primitive types:
byte has 8 bits Integer -> from byte to long
short has 16 bits
int has 32 bits
long has 64 bit
float has 32 bits Real Number -> from float to double
double has 64 bits
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions