csc 185-seventh lab
TRANSCRIPT
-
8/14/2019 CSC 185-Seventh Lab
1/9
CSC 185
Lab - 7
-
8/14/2019 CSC 185-Seventh Lab
2/9
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.
-
8/14/2019 CSC 185-Seventh Lab
3/9
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;
}
}
-
8/14/2019 CSC 185-Seventh Lab
4/9
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);
}
}
-
8/14/2019 CSC 185-Seventh Lab
5/9
Java classes Programclass car
{
String color;int speed;
int gear;
car(int const_speed, int const_gear)
{
speed = const_speed;
gear = const_gear;
}car()
{
color = "RED";
speed = 80;
}
public void speedUp(int speedIncrement)
{ speed = speed + speedIncrement;
}
public void printCarDetails()
{
System.out.println("New Speed is:"+speed+" NewGear is:"+gear);
}
}
Constructors
Instance Methods
-
8/14/2019 CSC 185-Seventh Lab
6/9
public class Main {
public static void main(String[] args) {
cartoyota_instance = new car(10,2);
carBMW_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();
}
}
Creating Objects
-
8/14/2019 CSC 185-Seventh Lab
7/9
Constructors
In Java, if you want a piece of code to run whenyour class is called for the first time we define amethod in a class with the same name as that ofclass. Constructors cannot have return type.
The purpose of constructors are basically to setsome variables when object is created for thefirst time.
There can be more than one constructors in aclass, distinguished by their parameters. Whenthe class is initialized, Java will call the rightconstructor with matching arguments and type.
-
8/14/2019 CSC 185-Seventh Lab
8/9
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 users name. If the user namematches 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.
-
8/14/2019 CSC 185-Seventh Lab
9/9
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.