OBJECT-ORIENTED
PROGRAMMING WITH JAVA
NAME : JELLY ANAK JONATHAN
COURSE : AT47
STUDENT ID : D20131064637
EXERCISE 1
MAIN CLASS
package chapter10_array;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Person aa;
Person [] person;
aa = new Person();
aa.setName("Name");
aa.setAge(2);
aa.setGender( "g" );
String name, inpStr; int age; char gender;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name:"); name = scanner.next ( );
System.out.print("Enter age:"); age = scanner.nextInt( );
System.out.print("Enter gender:"); inpStr = scanner.next( );
gender = inpStr.charAt(0);
PERSON CLASS
package chapter10_array;
public class Person {
private String Name;
private int Age;
private String Gender;
public void Student (){}
public void setName(String w)
Name=w;
public void setAge(int b)
Age=b;
public void setGender(String c)
Gender=c;
public String getName()
return Name;
public int getAge()
return Age;
public String getGender()
return Gender;
}
EXERCISE 2
COMPARE TO
public class CompareTo {
public static void main(String args[]) {
String str1 = "String method tutorial";
String str2 = "compareTo method ";
String str3 = "String method tutorial";
int var1 = str1.compareTo( str2 );
System.out.println("str1 & str2 comparison: "+var1);
int var2 = str1.compareTo( str3 );
System.out.println("str1 & str3 comparison: "+var2);
int var3 = str2.compareTo("compareTo method");
System.out.println("str2 & string argument comparison: "+var3);
SUBSTRING
public class SubString{
public static void main(String args[]) {
String str= new String("quick brown fox jumps over the lazy dog");
System.out.println("Substring starting from index 15:");
System.out.println(str.substring(15));
System.out.println("Substring starting from index 15 and ending at 20:");
System.out.println(str.substring(15, 20));
}
END WITH
public class EndsWith{
public static void main(String args[]){
String str1 = new String("This is a test String");
String str2 = new String("Test ABC");
boolean var1 = str1.endsWith("String");
boolean var2 = str1.endsWith("ABC");
boolean var3 = str2.endsWith("String");
boolean var4 = str2.endsWith("ABC");
System.out.println("str1 ends with String: "+ var1);
System.out.println("str1 ends with ABC: "+ var2);
System.out.println("str2 ends with String: "+ var3);
System.out.println("str2 ends with ABC: "+ var4);
START WITH
public class StartsWith{
public static void main(String args[]) {
String str= new String("quick brown fox jumps over the lazy dog");
System.out.println("String str starts with quick: "+str.startsWith("quick"));
System.out.println("String str starts with brown: "+str.startsWith("brown"));
System.out.println("substring of str(starting from 6th index) has brown prefix: "
+str.startsWith("brown", 6));
System.out.println("substring of str(starting from 6th index) has quick prefix: "
+str.startsWith("quick", 6));
}
TRIM
public class Trim{
public static void main(String args[]){
String str = new String(" How are you?? ");
System.out.println("String before trim: "+str);
System.out.println("String after trim: "+str.trim());
VALUE OF
public class CopyValueOfExample {
public static void main(String args[]) {
char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
String str1 = "Text";
String str2 = "String";
//Variation 1:String copyValueOf(char[] data)
str1 = str1.copyValueOf(data);
System.out.println("str1 after copy: " + str1);
//Variation 2:String copyValueOf(char[] data,int offset,int count)
str2 = str2.copyValueOf(data, 5, 3 );
System.out.println("str2 after copy: " + str2);
}
EXERCISE 3
public class Assertion {
/**
* Precondition.
* @param condition we expect to be true.
* @param description repeats the condition or describes it in
* other words.
*/
public static void Require(boolean condition, String description) {
if (!condition) {
throw new RuntimeException("Precondition: The expectation '" + description + "' is violated");
/**
* Precondition.
* @param objectToBeTested is an object we expect to be not null
* @param objectName is the name of the variable we test.
*/
public static void RequireNotNull(Object objectToBeTested,String objectName) {
if (objectToBeTested == null) {
throw new RuntimeException("Precondition: The expectation '" + objectName + " is not null' is
violated");
/**
* Postcondition.
* @param condition we expect to be true.
* @param description repeats the condition or describes it in
* other words.
*/
public static void Ensure(boolean condition, String description) {
if (!condition) {
throw new RuntimeException("Postcondition: The expectation '" + description + "' is
violated");
/**
* Common condition to be used in the middle of methods
* @param condition we expect to be true.
* @param description repeats the condition or describes it in
* other words.
*/
public static void Check(boolean condition, String description) {
if (!condition) {
throw new RuntimeException("Condition: The expectation '" + description + "' is violated");
EXERCISE 4
CATCHER
import java.io.*;
public class ExcepTest{
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
System.out.println("Out of the block"); } }
PROPAGATOR
class TestExceptionPropagation1{
void m(){
int data=50/0;
void n(){
m();
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow...");