You are on page 1of 9

CSC 185

Lab - 7
Programs
• Write Java methods to calculate the area
of rectangle, area of circle, and area of a
square.
• Write a Java method ‘reverseNumber()’ to
reverse a number input.
Ex: Input 1234 should return 4321.
• Write a Java method to see if a number
input is even or odd.
package helloworld;

import java.util.Scanner;

public class Main


{
public static void main(String[] args)
{
System.out.println("I am in Main method");
System.out.println("Enter the radius to calculate the area of a circle");
Scanner s = new Scanner(System.in);
int r = s.nextInt();
System.out.println("Making a call to the method areaCircle");
double circle_area = areaCircle(r);
System.out.println("The area of circle is:"+circle_area);

}
public static double areaCircle(int radius)
{
System.out.println("I am in areaCircle method");
double area = 3.14 * radius * radius;
return area;
}

}
package helloworld;

import java.util.Scanner;

public class Main


{
public static void main(String[] args)
{
System.out.println("Enter a number and I reverse it for you!");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
reverseNumber(num);

public static void reverseNumber(int num)


{
int reversednum = 0;
int temp = 0;
while(num > 0)
{
temp = num % 10; // Gives me the last digit and stores in temp.
reversednum = reversednum * 10 + temp;
num = num/10; // Gives the num leaving last digit

System.out.println("Reversed number is:"+reversednum);


}
}
class car
Java classes Program
{
String color;
int speed;
int gear;

car(int const_speed, int const_gear)


{
speed = const_speed;
gear = const_gear;
Constructors

}
car()
{
color = "RED";
speed = 80;

public void speedUp(int speedIncrement)


{ Instance Methods
speed = speed + speedIncrement;
}

public void printCarDetails()


{
System.out.println("New Speed is:"+speed+" NewGear is:"+gear);
}
}
public class Main {

public static void main(String[] args) {


car toyota_instance = new car(10,2); Creating Objects
car BMW_instance = new car();

toyota_instance.speedUp(30);
BMW_instance.speedUp(20);
System.out.println("My toyota car details are:");
toyota_instance.printCarDetails();
System.out.println("BMW car details are:");
BMW_instance.printCarDetails();
}
}
Constructors
• In Java, if you want a piece of code to run when
your class is called for the first time we define a
method in a class with the same name as that of
class. Constructors cannot have return type.
• The purpose of constructors are basically to set
some variables when object is created for the
first time.
• There can be more than one constructors in a
class, distinguished by their parameters. When
the class is initialized, Java will call the right
constructor with matching arguments and type.
Program depicting classes in Java
• Write a java program for the following scenario:
There is a bank which consists of checking
accounts for 10 people. The program should ask
the user to input user’s name. If the user name
matches to the user name having an account in
the bank, the user should be prompted to enter
option 1(Check balance) 2: Add amount 3:
withdraw some money from the account 4:exit.
Announcements
• There will be an In-class quiz (CSC 185)
next Tuesday : 11/03/2009.
• Homework will be given tentatively for
CSC 201 on 10/29/09.
• There will be an In-class quiz on 11/05/09
for CSC 201.

You might also like