Ini adalah beberapa latihan kodingan java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| /** Latihan 8.7 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class Date
{
private int month; private int day; private int year;
private static final int[] daysPerMonth =
{0,31,28,31,30,31,30,31,31,30,31,30,31};
public Date(int theMonth, int theDay, int theYear)
{
month=checkMonth(theMonth); year=theYear; day=checkDay(theDay);
System.out.printf("Date object constructor for date %s\n",this);
}
private int checkMonth(int testMonth)
{
if(testMonth>0&&testMonth<=12)
return testMonth;
else
throw new IllegalArgumentException("Month Must be 1-12");
}
private int checkDay(int testDay)
{
if(testDay>0&&testDay<=daysPerMonth[month])
return testDay;
if(month==2&&testDay==29&&(year%400==0||(year%4==0&&year%100!=0)))
return testDay;
throw new IllegalArgumentException("day out-of-range for the specific month and year");
}
public String toString()
{
return String.format("%d/%d/%d",month,day,year);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| /** Latihan 8.8 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class Employee
{
private String firstName; private String lastName;
private Date birthDate; private Date hireDate;
public Employee(String first, String last, Date dateOfBirth, Date dateOfHire)
{
firstName=first; lastName=last;
birthDate=dateOfBirth; hireDate=dateOfHire;
}
public String toString()
{
return String.format("%s, %s Hired: %s Birthday: %s",lastName,firstName,hireDate,birthDate);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
| /**Latihan 8.9 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class EmployeeTest
{
public static void main(String [] args)
{
Date Birth=new Date (7,24,1949);
Date Hire= new Date(3,12,1968);
Employee employee= new Employee ("Bob","Blue",Birth,Hire);
System.out.println(employee);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| /** Latihan 8.10 @author (tegar satrio utomo) @version (6/3/17) */
public enum Book
{
JHTP("Java How to Program","2012"),
CHTP("C How to Program","2007"),
IW3HTP("Internet & World Wide Web How to Program","2008"),
CPPHTP("C++ How to Program","2012"),
VBHTPC("Visual Basic How to Program","2011"),
CSHARPHTP("Visual C# How to Program","2011");
private final String title;
private final String copyrightYear;
Book(String BookTitle, String year)
{
title=BookTitle;
copyrightYear=year;
}
public String getTitle()
{
return title;
}
public String getCopyrightYear()
{
return copyrightYear;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| /** Latihan 8.11 @author (Tegar Satrio Utomo) @version (6/3/17) */
import java.util.EnumSet;
public class EnumTest
{
public static void main(String[] args)
{
System.out.println("All Books\n");
for(Book book : Book.values() )
System.out.printf("%-10s%-45s%s\n",book,book.getTitle(),book.getCopyrightYear());
System.out.println("\nDisplay a range of enum constants:\n");
for(Book book : EnumSet.range(Book.JHTP,Book.CPPHTP))
System.out.printf("%-10s%-45s%s\n",book,book.getTitle(),book.getCopyrightYear());
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| /** Latihan 8.12 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class Employee
{
private String firstName;
private String lastName;
private static int count= 0;
public Employee(String first, String last)
{
firstName = first;
lastName = last;
++count;
System.out.printf("Employee constructor: %s %s; count = %d\n",firstName,lastName,count);
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public static int getCount()
{
return count;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| /** latihan 8.13 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class EmployeeTest
{
public static void main(String[] args)
{
System.out.printf("Employees before instatiation: %d\n",Employee.getCount());
Employee e1 = new Employee("Susan","Baker");
Employee e2 = new Employee("Bob","Blue");
System.out.println("\nEmployees after instantion: ");
System.out.printf("via e1.getCount(): %d\n",e1.getCount());
System.out.printf("via e2.gerCount(): %d\n",e2.getCount());
System.out.printf("via employee.getCount(): %d\n",Employee.getCount());
System.out.printf("Employee 1: %s %s\nEmployee 2: %s %s\n"
,e1.getFirstName(),e1.getLastName(),e2.getFirstName(),e2.getLastName());
e1=null;e2=null;
}
}
|
Anda baru saja membaca artikel yang berkategori
PBO
dengan judul
A Deeper Look - PBO. Jika kamu suka, jangan lupa like dan bagikan keteman-temanmu ya... By :
Faruqi's Blog
Artikel Menarik Lainnya : PBO
Ditulis oleh:
Etri -
Senin, 13 Maret 2017
Belum ada komentar untuk "A Deeper Look - PBO"
Posting Komentar