├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── Assignments.MD
├── HibernateConfiguration.MD
├── IndustrialJava.MD
├── README.md
├── SpringDependInjection.md
├── SpringDi.md
├── documents
└── GroovyEssential
├── forms-1.3.0.jar
├── miglayout15-swing.jar
├── resource
├── assignments
│ ├── Assignment 1.pdf
│ └── Assignment2.txt
├── lib
│ ├── joda-time-2.8.2.jar
│ └── mysql-connector-java-5.1.6.jar
├── logout.png
├── pressit.jpg
└── reference
│ └── WebCollection.docx
└── src
├── Area.java
├── ArgsMain.java
├── CharDemo.java
├── Course.java
├── DataTypePreSuf.java
├── Elapsed.java
├── ExecDemo.java
├── HelloWorld.java
├── HelloWorldDialog.java
├── MathTest.java
├── OperatorTest.java
├── Sin30.java
├── StringTest.java
├── Student.java
├── TypeCast.java
├── assignments
└── Assignment1.java
├── collection
├── ArrayListExample.java
├── CollectionSort.java
├── HashMapExample.java
├── HashSetExample.java
├── LinkedHashMapExample.java
├── LinkedHashSetExample.java
├── TreeMapExample.java
├── TreeSetExample.java
├── User.java
├── UserArrayList.java
└── VectorExample.java
├── control
├── Address.java
├── BigDecimalIntTest.java
├── Break.java
├── BreakLoop4.java
├── Continue.java
├── ControlStat.java
├── Currency.java
├── CurrencyEnumTest.java
├── Day.java
├── DoWhile.java
├── Emp.java
├── FindMinPassArr.java
├── ForEachDemo.java
├── HelloConstTest.java
├── IfElse.java
├── RetOb.java
├── Return.java
├── SwitchDemo.java
├── SwitchDemo1.java
├── User.java
├── WhileComma.java
└── WhileDemo.java
├── datereflection
├── DateFormatTest.java
├── DateStrToDateObject.java
├── DateTest.java
├── InstanceOfUses.java
├── JodaTimeExample.java
├── JodaTimeExamples.java
├── ReflectionClassName.java
└── SimpleDateFormatTest.java
├── dt
├── AssignmentTest.java
├── Light.java
└── Scope.java
├── exhand
├── CompileTimeException.java
├── CustomExceptionTest.java
├── FinallyTest.java
├── InvalidAgeException.java
├── MultiCatchException.java
├── MultiTryCatch.java
├── PgmWithoutException.java
├── RuntimeExceptionAutoPropagate.java
├── ThrowCheckExceptionCompileError.java
├── ThrowExampleWithTryCatch.java
├── ThrowTest.java
└── ThrowsUncheckPropagation.java
├── innerclass
├── ActionEvent.java
├── ActionListener.java
├── AnnInnAbstClass.java
├── AnnonymousInterfaceImpl.java
├── ButtonClick.java
├── ButtonClickActionListener.java
├── EmployeeOuter.java
├── Person.java
├── ShyamEat.java
├── StaticInnerClassWithStaticMethod.java
└── StaticInnerO.java
├── io
├── BufferedInputStreamExample.java
├── BufferedOutputStreamExample.java
├── BufferedReaderInputStreamReader.java
├── DeserializeUser.java
├── FileExample.java
├── FileInputStreamExample.java
├── FileOutputStreamExample.java
├── FileReaderExample.java
├── FileStreamReadOneWriteToOther.java
├── FileWriterExample.java
├── ReadCsv.java
├── ScannerConsoleInput.java
├── ScannerDelimated.java
├── ScannerEx.java
├── SerializationUser.java
├── TryWithResource.java
└── User.java
├── jdbc
├── DeleteStatement.java
├── InsertStatement.java
├── JDBConnection.java
├── JdbcConnectionTest.java
├── MysqlConnection.java
└── UpdateStatment.java
├── mthread
├── FacebookLike.java
├── RunnableInterface.java
├── ShutdownHookExample.java
├── SynchronizeFbLike.java
├── ThreadClass.java
├── ThreadJoinCalcTanx.java
├── ThreadJoinPrintValues.java
└── ThreadsWithName.java
├── oop
├── ChangeStaticValue.java
├── CounterWithStatic.java
├── CounterWithoutStatic.java
├── PassObjAsParam.java
├── PrintThisKeyword.java
├── StaticBlock.java
├── StaticVariable.java
├── VechicleTestMain.java
├── abst
│ ├── AbstractClassTest.java
│ ├── Bank.java
│ ├── Nabil.java
│ ├── NicAsia.java
│ └── StandChart.java
├── block
│ └── Bike.java
├── finalex
│ ├── EmployeePanNo.java
│ └── FinalParam.java
├── inheritence
│ ├── Bus.java
│ ├── Car.java
│ └── Vehicle.java
├── interf
│ ├── Bank.java
│ ├── InterfaceImplTest.java
│ ├── Nabil.java
│ ├── NicAsia.java
│ ├── Printable.java
│ ├── PrintableImpl.java
│ ├── StandChart.java
│ └── ext
│ │ ├── InterfaceInheritenceTest.java
│ │ ├── Printable.java
│ │ ├── Readable.java
│ │ └── Showable.java
├── overloading
│ ├── AmbigiousCalculation.java
│ ├── Calculation.java
│ ├── Calculation2.java
│ └── OverloadingTypePromotionCalc.java
├── override
│ ├── Bike.java
│ ├── Vehicle.java
│ └── bank
│ │ ├── Bank.java
│ │ ├── InterestRateTest.java
│ │ ├── Nabil.java
│ │ ├── NicAsia.java
│ │ └── StandChart.java
├── polymorphism
│ ├── Bike.java
│ ├── Splender.java
│ ├── binding
│ │ ├── Boy.java
│ │ └── Human.java
│ ├── multilevel
│ │ ├── Animal.java
│ │ ├── BabyDog.java
│ │ └── Dog.java
│ └── runtime
│ │ ├── Bank.java
│ │ ├── Nabil.java
│ │ ├── NicAsia.java
│ │ ├── RuntimePolymTest.java
│ │ └── StandChart.java
└── superex
│ ├── Bike.java
│ └── Vehicle.java
├── org
├── Course.java
├── EmployeeChild.java
├── EmployeeTest.java
├── Factorial.java
├── accm
│ ├── Employee.java
│ ├── EmployeeMain.java
│ ├── Student.java
│ ├── User.java
│ └── UserMain.java
├── constructor
├── course
│ ├── AndroidCourse.java
│ ├── JavaCourse.java
│ └── StaticImport.java
├── imports
│ ├── service
│ │ ├── BigDecimal.java
│ │ └── UserService.java
│ └── util
│ │ ├── FileUtil.java
│ │ └── MathUtils.java
├── multiclassex
│ ├── InnerClassEx.java
│ └── MultipleClassEx.java
├── pkgpvt
│ ├── JavaCourse.java
│ └── Student.java
├── stud
│ └── Student.java
└── typeconversion
│ ├── HelloWorld.class
│ └── VarInitClass.java
├── pojo
├── StudPojoMain.java
└── StudentPojo.java
├── str
├── Format.java
├── StringBuffMain.java
├── StringBufferTest.java
├── StringEqual.java
└── WrapperIntro.java
├── swing
├── EventHandlingAdapter.java
├── EventHandlingListener.java
├── ExtendtedJFrame.java
├── ImageButton.java
├── LoginScreenLayout.java
├── SwingComponets.java
├── SwingContentPaneExample.java
├── SwingWindowExample.java
├── SwingWindowsBuilderTest.java
├── WindowsBuilderFirst.java
└── proj
│ ├── LoginScreenLayout.java
│ ├── StudentMangement.java
│ └── WindowManager.java
├── test
└── java
│ ├── JButtonTableExample.java
│ ├── SpinnerTest.java
│ ├── Tab.java
│ ├── TableTest.java
│ └── TableWithSpinner.java
└── util
└── CalculatorConsole.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | JavaStud
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/CharDemo.java=UTF-8
3 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.7
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.7
12 |
--------------------------------------------------------------------------------
/Assignments.MD:
--------------------------------------------------------------------------------
1 | OOP:
2 |
3 | Factory patteren:
create currency interface implemnentation classs with all possible currency types. Now create factory method to determine curreycy type and display it.
4 | Q1: http://javarevisited.blogspot.com/2011/12/factory-design-pattern-java-example.html
5 |
6 |
7 | https://www.cs.utexas.edu/~scottm/cs307/codingSamples.htm
8 |
--------------------------------------------------------------------------------
/HibernateConfiguration.MD:
--------------------------------------------------------------------------------
1 | #Hibernate Configurations:
2 | ####1. POM.xml Dependencies
3 | ```
4 |
5 |
6 |
7 | junit
8 | junit
9 | 3.8.1
10 | test
11 |
12 |
13 | org.hibernate
14 | hibernate-core
15 | 4.0.0.Final
16 |
17 |
18 | mysql
19 | mysql-connector-java
20 | 5.1.10
21 |
22 |
23 |
24 | ```
25 | ####2. src/main/resources/hibernate.cfg.xml file:
26 | ```
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | com.mysql.jdbc.Driver
36 | jdbc:mysql://localhost:3306/test
37 | root
38 |
39 |
40 |
41 | 1
42 |
43 |
44 | org.hibernate.dialect.MySQLDialect
45 |
46 |
47 | thread
48 |
49 |
50 | org.hibernate.cache.NoCacheProvider
51 |
52 |
53 | true
54 | validate
55 |
56 |
57 |
58 |
59 |
60 |
61 | ```
62 | ####3. Get Hibernate Session:
63 | ```
64 | import java.sql.Date;
65 |
66 | import org.hibernate.Session;
67 | import org.hibernate.SessionFactory;
68 | import org.hibernate.cfg.Configuration;
69 | -------------------------------
70 | SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
71 | ------------------------------
72 | Session session = sessionFactory.openSession();
73 | ---------------Save-----------
74 | session.beginTransaction();
75 |
76 | Long id = (Long) session.save(emp);
77 |
78 | session.getTransaction().commit();
79 |
80 | session.close();
81 | --------------GET--------------------
82 | Employee emp = (Employee) session.get(Employee.class, 8L);
83 | ---------------UPDATE----------------
84 | session.beginTransaction();
85 |
86 | Employee emp = (Employee) session.get(Employee.class, 8L);
87 | emp.setLastname("Joshi");
88 |
89 | session.update(emp);
90 |
91 | session.getTransaction().commit();
92 | session.close();
93 | -------------------DELETE---------------------
94 | session.beginTransaction();
95 |
96 | Employee employee = (Employee) session.get(Employee.class, 9L);
97 | session.delete(employee);
98 |
99 | session.getTransaction().commit();
100 |
101 | session.close();
102 | ----------------------------------------
103 | ```
104 | https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html
105 |
--------------------------------------------------------------------------------
/IndustrialJava.MD:
--------------------------------------------------------------------------------
1 | Industrial Java Development With Advance Web (Training)
2 | Contact : email: yadabrajojha@gmail.com
3 | Phone No: +977-9841664352
4 |
5 | Syllabus:
6 |
7 | 0. Intellij IDE
8 | 1. Git
9 | 2. SourceTree
10 | 3. Github OR GitLab
11 | 4. Java8 > Lamda expression, DateTime, Nashron
12 | 5. Spring
13 | 6. Spring MVC( xml based)
14 | 7. Spring security (annotation based/ xml based)
15 | 8. Session maintenance + spring login screen
16 | 9. Spring form + form validation
17 | 10. Spring Web Editor
18 | 11. Spring Boot
19 | 12. Spring Initializr
20 | 13. Spring Social
21 | 14. Spring Rest
22 |
23 | 15. How to show uploaded image (Profile photo + folder photos)
24 | 16. Upload multiple file
25 |
26 | 17. Hibernate: relation, cascading, BaseEntity, transaction
27 | 18. Hibernate Criteria, custom query , HQL
28 |
29 | 19. Maven Commands: plugins
30 |
31 | 20. Project management tool: Agile Tool like JIRA OR Trello.com
32 |
33 |
34 | 21. Jquery, Jquery dom manipulation, DataTable, Server side pagination using hibernate
35 |
36 | 22. Angular JS basic, Controller , scope, routing, Server call , templates, directives, services, filters
37 |
38 | 23. Reports ( iReport), HTML5 Reports, CSV
39 | 24. Big Data
40 | 25. HTML5 , CSV, 3D.js(https://d3js.org/), pdf.js,
41 | 26. Font awesome,
42 | 27.
43 | 28. Bootstrap, templates
44 | 29.
45 | 30. JSON, Rest Call
46 |
47 | 31. FTP server basic(client), putty, VPN
48 |
49 | 32. Jquery Lighterbox
50 | 33. ToopTip
51 | 34. Touch enabled jQuery plugin that lets you create beautiful responsive carousel slider.
52 | http://owlgraphic.com/owlcarousel/
53 | https://github.com/abeMedia/jquery-smoove
54 |
55 |
56 |
57 |
58 | Project work same like intern in the software company.
59 |
60 |
61 |
--------------------------------------------------------------------------------
/SpringDependInjection.md:
--------------------------------------------------------------------------------
1 | ```
2 |
3 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Kathmandu
14 | Dhangadi
15 | Mahendranagar
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | ```
36 |
37 | Make sure you have spring-beans framework is also in pom.xml file.
38 | ```
39 |
40 | org.springframework
41 | spring-beans
42 | ${spring-framework.version}
43 |
44 | ```
45 |
--------------------------------------------------------------------------------
/SpringDi.md:
--------------------------------------------------------------------------------
1 | '''
2 |
3 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Kathmandu
14 | Dhangadi
15 | Mahendranagar
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | '''
38 |
--------------------------------------------------------------------------------
/forms-1.3.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/forms-1.3.0.jar
--------------------------------------------------------------------------------
/miglayout15-swing.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/miglayout15-swing.jar
--------------------------------------------------------------------------------
/resource/assignments/Assignment 1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/assignments/Assignment 1.pdf
--------------------------------------------------------------------------------
/resource/assignments/Assignment2.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/assignments/Assignment2.txt
--------------------------------------------------------------------------------
/resource/lib/joda-time-2.8.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/lib/joda-time-2.8.2.jar
--------------------------------------------------------------------------------
/resource/lib/mysql-connector-java-5.1.6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/lib/mysql-connector-java-5.1.6.jar
--------------------------------------------------------------------------------
/resource/logout.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/logout.png
--------------------------------------------------------------------------------
/resource/pressit.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/pressit.jpg
--------------------------------------------------------------------------------
/resource/reference/WebCollection.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/resource/reference/WebCollection.docx
--------------------------------------------------------------------------------
/src/Area.java:
--------------------------------------------------------------------------------
1 |
2 | public class Area {
3 | int length;
4 | int breadth;
5 |
6 | void calcArea(){
7 | int area= length * breadth;
8 | System.out.println("Area is: " + area);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/ArgsMain.java:
--------------------------------------------------------------------------------
1 | public class ArgsMain {
2 | public static void main( String[] args ) {
3 |
4 | System.out.println( args[ 0 ] );
5 |
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/CharDemo.java:
--------------------------------------------------------------------------------
1 |
2 | public class CharDemo {
3 | public static void main(String[] args) {
4 | char x = 88;
5 | char y = 'Y';
6 |
7 | char a = 'अ';
8 | System.out.println(x + " "+ y + " "+ a);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Course.java:
--------------------------------------------------------------------------------
1 |
2 | /**
3 | * This class provide java courses.
4 | * @author ojhay
5 | * @since JDK1.0
6 | */
7 | public class Course {
8 |
9 | /*
10 | This is varible
11 | to hold
12 | java course.
13 | */
14 | private String jCourse = "JAVA"; //instance variable
15 |
16 | public static final double PI = 3.14; //static variable
17 |
18 | /*
19 | * This is
20 | * method
21 | * to show
22 | * java
23 | * course
24 | */
25 | public void showJavaCourse() {
26 | System.out.println("Course Name is: " + jCourse); //JAVA
27 |
28 | //This is java course.
29 | int area = 500; //Local variable.
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/DataTypePreSuf.java:
--------------------------------------------------------------------------------
1 | public class DataTypePreSuf {
2 | public static void main( String[] args ) {
3 |
4 | System.out.println( 0XF );
5 |
6 | System.out.println( 010 );
7 |
8 | System.out.println( 0b1001 );
9 |
10 | int no = 1_000_000;
11 | System.out.println( no );
12 |
13 | long l = 100L;
14 | System.out.println( l );
15 |
16 | float f = 98.6f;
17 | System.out.println( f );
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Elapsed.java:
--------------------------------------------------------------------------------
1 | public class Elapsed {
2 | public static void main( String[] args ) {
3 | long start, end;
4 |
5 | System.out.println( "Timing a for loop from 0 to 100,000,000" );
6 |
7 | // time a for loop from 0 to 100,000,000
8 | start = System.currentTimeMillis( ); // get starting time
9 |
10 | for ( long i = 0; i < 100000000L; i++ )
11 | ;
12 |
13 | end = System.currentTimeMillis( ); // get ending time
14 |
15 | System.out.println( "Elapsed time: " + ( end - start ) );
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/ExecDemo.java:
--------------------------------------------------------------------------------
1 | public class ExecDemo {
2 |
3 | public static void main( String[] args ) {
4 |
5 | try {
6 |
7 | Runtime r = Runtime.getRuntime( );
8 | r.exec( "calc" );
9 |
10 | } catch ( Exception e ) {
11 | System.out.println( "Error executing notepad." );
12 | }
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/HelloWorld.java:
--------------------------------------------------------------------------------
1 | public class HelloWorld {
2 | public static void main( String[] args ) {
3 | System.out.println( "Hello World" );
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/src/HelloWorldDialog.java:
--------------------------------------------------------------------------------
1 | import javax.swing.JOptionPane;
2 |
3 | public class HelloWorldDialog {
4 | public static void main( String[] args ) {
5 |
6 | System.err.println( "Hello Every one" );
7 |
8 | System.out.println( "Hello World" );
9 |
10 | JOptionPane.showMessageDialog( null, "HELLO WORLD" );
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/MathTest.java:
--------------------------------------------------------------------------------
1 |
2 | public class MathTest {
3 | public static void main(String[] args) {
4 | double sq = Math.sqrt(4);
5 | System.out.println(sq);
6 |
7 | double z = Math.pow(2, 8);
8 | System.out.println(z);
9 |
10 | // sin90 = 1
11 | /*
12 | * 180 deg = PI radian
13 | * 1 deg = PI/180
14 | * 90 deg = PI/180 * 90
15 | * = PI/2
16 | */
17 | double sin90 = Math.sin(Math.PI / 2);
18 | System.out.println(sin90);
19 |
20 | double r = 9.8;
21 | double roundUpVal = Math.round(r);
22 | System.out.println("Round up: "+ roundUpVal);
23 |
24 | double floorVal = Math.floor(r);
25 | System.out.println("After Round: " + floorVal);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/OperatorTest.java:
--------------------------------------------------------------------------------
1 |
2 | public class OperatorTest {
3 | public static void main(String[] args) {
4 |
5 | int x = 5;
6 | System.out.println(x++); // 5
7 | System.out.println(x); // 6
8 |
9 | int y = 10;
10 | System.out.println(++y); // 11
11 |
12 | System.out.println((17 % 3));
13 |
14 | int z = 20;
15 |
16 | z = z + 50; // 70
17 | z += 50; // z = z + 50;
18 |
19 | z *= 2; // z = z * 2;
20 |
21 | // ==== &&===
22 | int scimarks = 75;
23 | if (scimarks > 0 && scimarks <= 100) {
24 |
25 | if (scimarks >= 40) {
26 | System.out.println("Ram is pass in science!");
27 | } else {
28 | System.out.println("Ram is failed in science!");
29 | }
30 |
31 | }
32 |
33 | // =======ternary operator(? :)==========
34 | boolean isPassed;
35 | int mathMarks = 55;
36 |
37 | // ---if else--
38 | if (mathMarks >= 40) {
39 | isPassed = true;
40 | } else {
41 | isPassed = false;
42 | }
43 | System.out.println("IS ram passed in Math: " + isPassed);
44 |
45 | // ? : ternary operator
46 |
47 | isPassed = (mathMarks >= 40) ? true : false;
48 | System.out.println("IS ram passed in Math: " + isPassed);
49 |
50 |
51 | // ========instanceof check Object type Class.=====
52 | String s = "ABC";
53 | if (s instanceof String) {
54 |
55 | }
56 |
57 | // Primitive: int Integer => use always Wrapper class.
58 | Integer i = 55;
59 | if (i instanceof Integer) {
60 |
61 | }
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Sin30.java:
--------------------------------------------------------------------------------
1 | public class Sin30 {
2 | public static void main( String[] args ) {
3 |
4 | double sin30 = Math.sin( Math.PI / 6 );
5 |
6 | System.out.println( sin30 );
7 |
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/StringTest.java:
--------------------------------------------------------------------------------
1 | public class StringTest {
2 | public static void main( String[] args ) {
3 |
4 | // // TEST 1
5 | // char[] ca = { 'j', 'a', 'v', 'a' };
6 | // String caStr = new String( ca );
7 | // System.out.println( caStr );
8 | //
9 | // // TEST 2
10 | // String s = "JAVA";
11 | // System.out.println( s );
12 | //
13 | // // Test 3:
14 | // System.out.println( "JAVA3" );
15 | //
16 | // // TEST 4:
17 | // String h1 = "HELLO";
18 | // int l = h1.length( );
19 | // System.out.println( l );
20 | //
21 | // System.out.println( "HELLO".length( ) );
22 | //
23 | // // TEST 4:
24 | // System.out.println( "HELLO".toLowerCase( ) );
25 | //
26 | // System.out.println( "HELLO".indexOf('L', 3 ) );
27 |
28 | // String j = "JAVA";
29 | // System.out.println( j.concat( "Student" ) );
30 | // System.out.println( j );
31 | //
32 | // // ======
33 | // String s = "HELLO";
34 | // s = s.concat( "WORLD" );
35 | // System.out.println( s );
36 | //
37 | // // --------------
38 | // String str = "BSC";
39 | // String rs = str.concat( "COMP SC" );
40 | // System.out.println( rs );
41 |
42 | // ----------------------------
43 | // String s1 = "Java string split method";
44 | // String[] words = s1.split( "\\s" );
45 | // for ( int i = 0; i < words.length; i++ ) {
46 | // System.out.println( words[ i ] );
47 | // }
48 |
49 | // ========================
50 |
51 | String s = "HELLO";
52 | System.out.println( s );
53 | System.out.println( s.toString( ) );
54 |
55 | }
56 | }
--------------------------------------------------------------------------------
/src/Student.java:
--------------------------------------------------------------------------------
1 |
2 | public class Student {
3 | public static void main(String[] args) {
4 |
5 | //No 1:
6 | Course c = new Course();
7 | c.showJavaCourse();
8 |
9 | // String s = c.jCourse;
10 | // System.out.println(s);
11 |
12 | //No 2: Direct Access: ClassName.
13 | System.out.println(Course.PI);
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/TypeCast.java:
--------------------------------------------------------------------------------
1 | public class TypeCast {
2 | public static void main( String[] args ) {
3 | double x = 9.997;
4 | int nx = ( int ) x; // 9
5 |
6 | System.out.println( nx );
7 |
8 | byte b = 50;
9 | b = ( byte ) ( b * 2 );
10 |
11 | int i = 5000;
12 | float f = 5.67f;
13 |
14 | float result = i * f;
15 |
16 | System.out.println( result );
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/assignments/Assignment1.java:
--------------------------------------------------------------------------------
1 | package assignments;
2 |
3 | public class Assignment1 {
4 | public static void main(String[] args) {
5 | int sum = 0;
6 | for (int i = 0; i < args.length; i++) {
7 | sum= sum + Integer.parseInt(args[i]);
8 | }
9 | double avg= sum/args.length;
10 |
11 | System.out.println("Sum: " + sum + " Avg: "+ avg);
12 |
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/collection/ArrayListExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Iterator;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | public class ArrayListExample {
8 | public static void main( String[] args ) {
9 |
10 | List< String > countryList = new LinkedList< String >( );
11 | countryList.add( "Nepal" );
12 | countryList.add( "China" );
13 | countryList.add( "USA" );
14 | countryList.add( "Japan" );
15 |
16 | if ( countryList.contains( "Japan" ) ) {
17 | System.out.println( "Yes there is Japan." );
18 | }
19 |
20 | // Iterator can be used to traverse.
21 | Iterator< String > itr = countryList.iterator( );
22 | while ( itr.hasNext( ) ) {
23 | String s = itr.next( );
24 | System.out.println( s );
25 |
26 | // we can remove elements inside iterator.
27 | if ( s.equals( "China" ) ) {
28 | itr.remove( );
29 | }
30 | }
31 |
32 | countryList.remove( 2 );
33 | String npl = countryList.get( 0 );
34 |
35 | System.out.println( "No of elments left: " + countryList.size( ) );
36 | // Foreach: display purpose
37 | for ( String s: countryList ) {
38 | System.out.println( s );
39 | }
40 |
41 | for ( int i = 0; i < countryList.size( ); i++ ) {
42 | String s = countryList.get( i );
43 | countryList.remove( i );
44 | System.out.println( s );
45 | }
46 |
47 | countryList.clear( );
48 | System.out.println( countryList.isEmpty( ) );
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/collection/CollectionSort.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | public class CollectionSort {
8 | public static void main( String[] args ) {
9 |
10 | String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
11 |
12 | // Create and display a list containing the suits array elements
13 | List< String > list = Arrays.asList( suits ); // create List
14 |
15 | System.out.printf( "Unsorted array elements: %s\n", list );
16 |
17 | Collections.sort( list, Collections.reverseOrder() ); // sort ArrayList
18 |
19 | // Output list
20 | System.out.printf( "Sorted array elements: %s\n", list );
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/collection/HashMapExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.HashMap;
4 | import java.util.Iterator;
5 | import java.util.Map;
6 | import java.util.Map.Entry;
7 | import java.util.Set;
8 |
9 | public class HashMapExample {
10 | public static void main( String[] args ) {
11 |
12 | /* This is how to declare HashMap */
13 | HashMap< Integer, String > hmap = new HashMap< Integer, String >( );
14 |
15 | /* Adding elements to HashMap */
16 | hmap.put( 12, "Core Java" );
17 | hmap.put( 2, "Adv Java" );
18 | hmap.put( 7, "Spring" );
19 | hmap.put( 49, "Hibernate" );
20 | hmap.put( 3, "Maven" );
21 |
22 | /* Get values based on key */
23 | String var = hmap.get( 2 ); // Key:2
24 | System.out.println( "Value for key:2 is: " + var + "\n" );
25 |
26 | /* Remove values based on Key:3 */
27 | hmap.remove( 3 );
28 |
29 | /* Display content using Iterator */
30 | Set< Entry< Integer, String > > set = hmap.entrySet( );
31 | Iterator< Entry< Integer, String > > iterator = set.iterator( );
32 | while ( iterator.hasNext( ) ) {
33 | Map.Entry< Integer, String > me = iterator.next( );
34 | System.out.print( "Key is: " + me.getKey( ) + " & Value is: " + me.getValue( ) + "\n" );
35 | }
36 |
37 | /*Easy way*/
38 | System.out.println("------------");
39 | for(Entry hm: hmap.entrySet()){
40 | System.out.println(hm.getKey() + " : " + hm.getValue());
41 | }
42 |
43 | //to string works even easier
44 | System.out.println(hmap);
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/collection/HashSetExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.HashSet;
4 | import java.util.Iterator;
5 |
6 | public class HashSetExample {
7 | public static void main( String[] args ) {
8 |
9 | HashSet< String > countryset = new HashSet< String >( );
10 |
11 | countryset.add( "Nepal" );
12 | countryset.add( "China" );
13 | countryset.add( "Nepal" );
14 | countryset.add( "Japan" );
15 | countryset.add( "USA" );
16 |
17 | Iterator< String > itr = countryset.iterator( );
18 | while ( itr.hasNext( ) ) {
19 | System.out.println( itr.next( ) );
20 | }
21 |
22 | countryset.remove( "Japan" );
23 |
24 | System.out.println( "********" );
25 | for ( String country: countryset ) {
26 | System.out.println( country );
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/collection/LinkedHashMapExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Iterator;
4 | import java.util.LinkedHashMap;
5 | import java.util.Map;
6 | import java.util.Set;
7 | import java.util.Map.Entry;
8 |
9 | public class LinkedHashMapExample {
10 | public static void main( String[] args ) {
11 |
12 | // HashMap Declaration
13 | LinkedHashMap< Integer, String > lhmap = new LinkedHashMap< Integer, String >( );
14 |
15 | // Adding elements to LinkedHashMap
16 | lhmap.put( 22, "Core Java" );
17 | lhmap.put( 33, "Adv Java" );
18 | lhmap.put( 1, "Spring" );
19 | lhmap.put( 2, "Hibernate" );
20 | lhmap.put( 100, "Maven" );
21 |
22 | // Generating a Set of entries
23 | Set< Entry< Integer, String > > set = lhmap.entrySet( );
24 |
25 | // Displaying elements of LinkedHashMap
26 | Iterator< Entry< Integer, String > > iterator = set.iterator( );
27 | while ( iterator.hasNext( ) ) {
28 | Map.Entry< Integer, String > me = iterator.next( );
29 | System.out.print( "Key is: " + me.getKey( ) + "& Value is: " + me.getValue( ) + "\n" );
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/collection/LinkedHashSetExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Arrays;
4 | import java.util.LinkedHashSet;
5 | import java.util.Set;
6 |
7 | public class LinkedHashSetExample {
8 | public static void main( String[] args ) {
9 |
10 | Set< Integer > linkedHashset = new LinkedHashSet< Integer >( );
11 | linkedHashset.addAll( Arrays.asList( 20, 10, 50, 75, 90 ) );
12 | System.out.println( linkedHashset );
13 |
14 | Set< String > sHashset = new LinkedHashSet< String >( );
15 | sHashset.addAll( Arrays.asList( "HELLO", "WORLD", "JAVA" ) );
16 | System.out.println( sHashset );
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/collection/TreeMapExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Iterator;
4 | import java.util.Map;
5 | import java.util.Set;
6 | import java.util.TreeMap;
7 |
8 | public class TreeMapExample {
9 | public static void main( String[] args ) {
10 |
11 | /* This is how to declare TreeMap */
12 | TreeMap< Integer, String > tmap = new TreeMap< Integer, String >( );
13 |
14 | /* Adding elements to TreeMap */
15 | tmap.put( 1, "Core Java" );
16 | tmap.put( 23, "Adv Java" );
17 | tmap.put( 70, "Spring" );
18 | tmap.put( 4, "Hibernate" );
19 | tmap.put( 2, "Maven" );
20 |
21 | /* Display content using Iterator */
22 | Set set = tmap.entrySet( );
23 | Iterator iterator = set.iterator( );
24 | while ( iterator.hasNext( ) ) {
25 | Map.Entry mentry = ( Map.Entry ) iterator.next( );
26 | System.out.print( "Key is: " + mentry.getKey( ) + " & Value is: " + mentry.getValue( ) + "\n" );
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/collection/TreeSetExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Arrays;
4 | import java.util.TreeSet;
5 |
6 | public class TreeSetExample {
7 | public static void main( String args[] ) {
8 |
9 | // TreeSet of String Type
10 | TreeSet< String > tset = new TreeSet< String >( );
11 |
12 | // Adding elements to TreeSet
13 | tset.add( "ABC" );
14 | tset.add( "String" );
15 | tset.add( "Test" );
16 | tset.add( "Pen" );
17 | tset.add( "Ink" );
18 | tset.add( "Jack" );
19 | System.out.println( tset );
20 |
21 | // TreeSet of Integer Type
22 | TreeSet< Integer > tset2 = new TreeSet< Integer >( );
23 |
24 | // Adding elements to TreeSet
25 | tset2.addAll( Arrays.asList( 88, 7, 101, 0, 3, 222 ) );
26 | System.out.println( tset2 );
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/collection/User.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.io.Serializable;
4 |
5 | public class User implements Serializable {
6 |
7 | private static final long serialVersionUID = 7031679283226403692L;
8 |
9 | private int id;
10 |
11 | private String name;
12 |
13 | public User( int id, String name ) {
14 | this.id = id;
15 | this.name = name;
16 | }
17 |
18 | public int getId( ) {
19 | return id;
20 | }
21 |
22 | public void setId( int id ) {
23 | this.id = id;
24 | }
25 |
26 | public String getName( ) {
27 | return name;
28 | }
29 |
30 | public void setName( String name ) {
31 | this.name = name;
32 | }
33 |
34 | @Override
35 | public String toString( ) {
36 | return id + " " + name;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/collection/UserArrayList.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class UserArrayList {
7 | public static void main( String[] args ) {
8 |
9 | List< User > userList = new ArrayList< User >( ); // or LinkedList
10 |
11 | userList.add( new User( 1, "User1" ) );
12 | userList.add( new User( 2, "User2" ) );
13 | userList.add( new User( 3, "User3" ) );
14 | userList.add( new User( 4, "User4" ) );
15 | userList.add( new User( 5, "User5" ) );
16 |
17 | System.out.println( "User at index 2: " + userList.get( 2 ) + "\n" );
18 |
19 | userList.add( 2, new User( 10, "New USER" ) );
20 |
21 | for ( User user: userList ) {
22 | System.out.println( user );
23 | }
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/collection/VectorExample.java:
--------------------------------------------------------------------------------
1 | package collection;
2 |
3 | import java.util.Enumeration;
4 | import java.util.Vector;
5 |
6 | public class VectorExample {
7 | public static void main( String[] args ) {
8 |
9 | Vector< String > vec = new Vector< String >( );
10 |
11 | /* Adding elements to a vector */
12 | vec.addElement( "Apple" );
13 | vec.addElement( "Orange" );
14 | vec.addElement( "Mango" );
15 | vec.addElement( "Fig" );
16 |
17 | System.out.println( "Size is: " + vec.size( ) );
18 |
19 | /* Display Vector elements */
20 | Enumeration< String > en = vec.elements( );
21 | System.out.println( "\nElements are:" );
22 | while ( en.hasMoreElements( ) ) {
23 | System.out.print( en.nextElement( ) + " " );
24 | }
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/control/Address.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class Address {
4 | String city;
5 | String state;
6 | String country;
7 |
8 | public Address( String city, String state, String country ) {
9 | this.city = city;
10 | this.state = state;
11 | this.country = country;
12 | }
13 |
14 | @Override
15 | public String toString( ) {
16 | return city + " " + state + " " + country;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/control/BigDecimalIntTest.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class BigDecimalIntTest {
6 | public static void main(String[] args) {
7 | BigDecimal dec1 = new BigDecimal("25.5");
8 | BigDecimal dec2 = BigDecimal.valueOf(25.5);
9 | BigDecimal total = dec1.add(dec2); // dec1 + dec2;
10 | System.out.println(total);
11 |
12 | if (total.intValue() > 100)
13 | System.out.println("total is greater then 100");
14 | else {
15 | System.out.println("Total is less then 100");
16 |
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/control/Break.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class Break {
4 |
5 | public static void main(String[] args) {
6 | boolean t = true;
7 | first: {
8 |
9 | second: {
10 | third: {
11 | System.out.println("Before the break.");
12 | if (t)
13 | break second; // break out of second block
14 | System.out.println("This won't execute");
15 | }
16 | System.out.println("This won't execute");
17 | }
18 |
19 | System.out.println("This is after second block.");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/control/BreakLoop4.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class BreakLoop4 {
4 | public static void main(String[] args) {
5 |
6 | outer: for (int i = 0; i < 3; i++) {
7 | System.out.print("Pass " + i + ": ");
8 |
9 | for (int j = 0; j < 100; j++) {
10 |
11 | if (j == 10)
12 | break outer; // exit both loops
13 |
14 | System.out.print(j + " ");
15 | }
16 | System.out.println("This will not print");
17 | }
18 |
19 | System.out.println("Loops complete.");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/control/Continue.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class Continue {
4 |
5 | public static void main( String[] args ) {
6 |
7 | for ( int i = 0; i < 10; i++ ) {
8 | if ( i % 2 == 0 ) { // remainder
9 | continue;
10 | }
11 | System.out.println( i );
12 | }
13 |
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/control/ControlStat.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class ControlStat {
4 |
5 | public static void main(String[] args) {
6 | int a = 10, b = 20;
7 | int i = 10, j = 3, k = 8;
8 | int c = 30, d = 50;
9 | if (i == 10) {
10 | if (j < 20)
11 | a = b;
12 | if (k > 100)
13 | c = d; // this if is
14 | else
15 | a = c; // associated with this else
16 | } else {
17 | a = d; // this else refers to if(i == 10)
18 | }
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/control/Currency.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public enum Currency {
4 |
5 | PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
6 |
7 | private int value;
8 |
9 | private Currency(int value) {
10 | this.value = value;
11 | }
12 |
13 | public int getValue(){
14 | return this.value;
15 | }
16 |
17 | public void printEnums(){
18 | for(Currency curren: values()){
19 | System.out.println(curren);
20 | }
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/control/CurrencyEnumTest.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class CurrencyEnumTest {
4 | public static void main(String[] args) {
5 |
6 | Currency currency = Currency.DIME;
7 |
8 | System.out.println( currency == Currency.PENNY );
9 |
10 | int dimeValue = Currency.DIME.getValue();
11 | System.out.println("Dime Value: "+ dimeValue);
12 |
13 | System.out.println(dimeValue);
14 |
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/control/Day.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public enum Day {
4 | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
5 | }
6 |
--------------------------------------------------------------------------------
/src/control/DoWhile.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | import java.util.Scanner;
4 |
5 | public class DoWhile {
6 |
7 | public static void main(String[] args) {
8 | System.out.print("Enter any number: ");
9 | Scanner scanner = new Scanner(System.in);
10 | int n = scanner.nextInt();
11 | do {
12 | System.out.println("tick " + n);
13 | n--;
14 | } while (n > 0);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/control/Emp.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class Emp {
4 | int id;
5 | String name;
6 | Address address;
7 |
8 | public Emp( int id, String name, Address address ) {
9 | this.id = id;
10 | this.name = name;
11 | this.address = address;
12 | }
13 |
14 | @Override
15 | public String toString( ) {
16 | return id + " " + name + " " + address;
17 | }
18 |
19 | public static void main( String[] args ) {
20 | Address add1 = new Address( "KTM", "Bagmati", "Nepal" );
21 | Emp emp1 = new Emp( 111, "Sam", add1 );
22 |
23 | System.out.println( emp1 );
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/control/FindMinPassArr.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class FindMinPassArr {
4 |
5 | public static void main( String[] args ) {
6 |
7 | int a[] = { 30, 10, 3, 7, 2 };
8 | min( a );
9 |
10 | int b[] = { 50, 10, 15 };
11 | min( b );
12 | }
13 |
14 | static void min( int arr[] ) {
15 |
16 | int min = arr[ 0 ]; // save min value in min variable.
17 |
18 | for ( int i = 0; i < arr.length; i++ ) {
19 |
20 | if ( arr[ i ] < min ) {// arr element is less then min value
21 | min = arr[ i ];// replace min value with arr element
22 | }
23 |
24 | }
25 | System.out.println( "Min Value: " + min );
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/control/ForEachDemo.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class ForEachDemo {
4 |
5 | public static void main( String[] args ) {
6 | int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
7 | int sum = 0;
8 | for ( int x: nums ) {
9 |
10 | if ( x == 5 ) {
11 | break;
12 | }
13 |
14 | sum += x;
15 | System.out.println( "x = " + x );
16 | }
17 | System.out.println( "Sum is: " + sum );
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/control/HelloConstTest.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 |
4 | public class HelloConstTest {
5 |
6 | private final String dy = "MONDAY";
7 |
8 | public static void main(String[] args) {
9 | HelloConstTest obj = new HelloConstTest();
10 | obj.printWeek(Day.MONDAY);
11 |
12 | //TEST EQUAL
13 | Day mnd = Day.MONDAY;
14 | boolean isEqual = (mnd == Day.MONDAY);
15 | System.out.println(isEqual);
16 |
17 | boolean isEq = (obj.dy == "ABC");
18 | boolean isEqual2 = (Day.SUNDAY == Day.MONDAY); //Error
19 | }
20 |
21 | public void printWeek(Day day){
22 | System.out.println("Day is: ");
23 | switch (day) {
24 | case SUNDAY:
25 | System.out.println(Day.SUNDAY);
26 | break;
27 | case MONDAY:
28 | System.out.println(Day.MONDAY);
29 | break;
30 |
31 | default:
32 | System.out.println("Day is not valid:");
33 | break;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/control/IfElse.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | class IfElse {
4 | public static void main(String args[]) {
5 |
6 | int month = 4; // April
7 | String season;
8 |
9 | if (month == 12 || month == 1 || month == 2) {
10 | season = "Winter";
11 |
12 | } else if (month == 3 || month == 4 || month == 5) {
13 | season = "Spring";
14 |
15 | } else if (month == 6 || month == 7 || month == 8) {
16 | season = "Summer";
17 |
18 | } else if (month == 9 || month == 10 || month == 11) {
19 | season = "Autumn";
20 |
21 | } else {
22 | season = "Bogus Month";
23 | }
24 |
25 | System.out.println("April is in the " + season + ".");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/control/RetOb.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | //Returning an object.
4 | class Test {
5 | int a;
6 | Test(int i) {
7 | a = i;
8 | }
9 | Test incrByTen() {
10 | Test temp = new Test(a + 10);
11 | return temp;
12 | }
13 | }
14 |
15 | class RetOb {
16 | public static void main(String args[]) {
17 | Test ob1 = new Test(2);
18 | Test ob2;
19 | ob2 = ob1.incrByTen();
20 | System.out.println("ob1.a: " + ob1.a);
21 | System.out.println("ob2.a: " + ob2.a);
22 | ob2 = ob2.incrByTen();
23 | System.out.println("ob2.a after second increase: " + ob2.a);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/control/Return.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Return {
6 |
7 |
8 | public static void main(String[] args) {
9 | //This program print something..nad do thsi.
10 | int[] a = { 6, 2, 3 };
11 | Arrays.sort(a);
12 | for(int a2 :a )
13 | System.out.println(a2);
14 |
15 | boolean t = true;
16 | System.out.println("Before the return.");
17 | if (t)
18 | return; // return to caller
19 | System.out.println("This won't execute.");
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/control/SwitchDemo.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class SwitchDemo {
4 |
5 | /*public void tellItLikeItIs(Day day) {
6 | switch (day) {
7 | case MONDAY:
8 | System.out.println("Mondays are bad.");
9 | break;
10 |
11 | case FRIDAY:
12 | System.out.println("Fridays are better.");
13 | break;
14 |
15 | case SATURDAY:
16 | case SUNDAY:
17 | System.out.println("Weekends are best.");
18 | break;
19 |
20 | default:
21 | System.out.println("Midweek days are so-so.");
22 | break;
23 | }
24 | }*/
25 |
26 | public static void main(String[] args) {
27 | int month = 25;
28 | int year = 2001;
29 | int numDays = 0;
30 |
31 | switch (month) {
32 | case 1:
33 | case 3:
34 | case 5:
35 | case 7:
36 | case 8:
37 | case 10:
38 | case 12:
39 | numDays = 31;
40 | break;
41 | case 4:
42 | case 6:
43 | case 9:
44 | case 11:
45 | numDays = 30;
46 | break;
47 | case 2:
48 | if ( ( (year % 4 == 0) && !(year % 100 == 0) ) || (year % 400 == 0) )
49 | numDays = 29;
50 | else
51 | numDays = 28;
52 | break;
53 | default:
54 | System.out.println("Invalid month.");
55 | break;
56 | }
57 | System.out.println("Number of Days = " + numDays);
58 |
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/control/SwitchDemo1.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | import java.util.Scanner;
4 |
5 |
6 | public class SwitchDemo1 {
7 | public static void main(String[] args) {
8 | System.out.print("Please intput a month: ");
9 |
10 | Scanner a = new Scanner(System.in);
11 | int month = a.nextInt();
12 |
13 | String monthString;
14 | switch (month) {
15 |
16 | case 1:
17 | case 2:
18 | case 3:
19 | monthString = "January";
20 | break;
21 | case 4:
22 | monthString = "April";
23 | break;
24 | case 5:
25 | monthString = "May";
26 | break;
27 | case 6:
28 | monthString = "June";
29 | break;
30 | case 7:
31 | monthString = "July";
32 | break;
33 | case 8:
34 | monthString = "August";
35 | break;
36 | case 9:
37 | monthString = "September";
38 | break;
39 | case 10:
40 | monthString = "October";
41 | break;
42 | case 11:
43 | monthString = "November";
44 | break;
45 | case 12:
46 | monthString = "December";
47 | break;
48 | default:
49 | monthString = "Invalid month";
50 | break;
51 | }
52 | System.out.println(monthString);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/control/User.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class User {
4 | int id;
5 | String name;
6 | Address address;
7 | }
8 |
--------------------------------------------------------------------------------
/src/control/WhileComma.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 | public class WhileComma {
4 | public static void main(String[] args) {
5 | int a, b;
6 | for (a = 1, b = 4; a < b; a++, b--) {
7 | System.out.println("a = " + a);
8 | System.out.println("b = " + b);
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/control/WhileDemo.java:
--------------------------------------------------------------------------------
1 | package control;
2 |
3 |
4 | public class WhileDemo {
5 | private static int num = 5;
6 |
7 | public static void main( String[] args ) throws InterruptedException {
8 |
9 | int n = num;
10 | while ( n > 0 ) {
11 | System.out.println( "tick " + n );
12 | n--;
13 | }
14 |
15 | for ( n = num; n > 0; n-- ) {
16 | System.out.println( "Tick: " + n );
17 | }
18 |
19 | int i = 0;
20 | for ( ;; ) {
21 | System.out.println( i++ );
22 | Thread.sleep( 1000 );
23 | if ( i > 10 ) {
24 | break;
25 | }
26 | }
27 |
28 | int nums[] = { 1, 2, 3, 4, 5 };
29 | for ( int j: nums ) {
30 | System.out.print( j + "\t" );
31 | }
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/datereflection/DateFormatTest.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import java.text.DateFormat;
4 | import java.util.Date;
5 |
6 | public class DateFormatTest {
7 | public static void main( String[] args ) {
8 |
9 | Date currentDate = new Date( );
10 | System.out.println( "Date: " + currentDate );
11 |
12 | String dateToStr = DateFormat.getInstance( ).format( currentDate );
13 | System.out.println( "Date: getInstance(): " + dateToStr );
14 |
15 | dateToStr = DateFormat.getDateInstance( ).format( currentDate );
16 | System.out.println( "Date: getDateInstance(): " + dateToStr );
17 |
18 | dateToStr = DateFormat.getTimeInstance( ).format( currentDate );
19 | System.out.println( "Date: getTimeInstance(): " + dateToStr );
20 |
21 | dateToStr = DateFormat.getDateTimeInstance( ).format( currentDate );
22 | System.out.println( "Date: getDateTimeInstance(): " + dateToStr );
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/datereflection/DateStrToDateObject.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Date;
6 |
7 | public class DateStrToDateObject {
8 | public static void main( String[] args ) {
9 |
10 | try {
11 |
12 | SimpleDateFormat formatter = new SimpleDateFormat( "dd/MM/yyyy" );
13 | Date date = formatter.parse( "31/03/2015" );
14 |
15 | System.out.println( "Date is: " + date );
16 |
17 | } catch ( ParseException e ) {
18 | e.printStackTrace( );
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/datereflection/DateTest.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 |
6 | public class DateTest {
7 | public static void main( String[] args ) {
8 |
9 | Date date1 = new Date( );
10 | System.out.println( date1 );
11 |
12 | long millis = System.currentTimeMillis( );
13 | Date date2 = new Date( millis );
14 | System.out.println( date2 );
15 |
16 | Date date3 = Calendar.getInstance( ).getTime( );
17 | System.out.println( date3 );
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/datereflection/InstanceOfUses.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | public class InstanceOfUses {
4 | public static void main( String[] args ) {
5 |
6 | displayObjectType( new Integer( "25" ) );
7 |
8 | displayObjectType( "25" );
9 | }
10 |
11 | public static void displayObjectType( Object o ) {
12 |
13 | if ( o instanceof Integer ) {
14 | System.out.println( "Parameter is Integer: " + o );
15 |
16 | } else if ( o instanceof String ) {
17 | System.out.println( "Parameter is String: " + o );
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/datereflection/JodaTimeExample.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import java.util.Date;
4 |
5 | import org.joda.time.DateTime;
6 |
7 | public class JodaTimeExample {
8 | public static void main( String[] args ) {
9 |
10 | DateTime dateTime = new DateTime( );
11 |
12 | Date date = dateTime.toDate( );
13 |
14 | System.out.println( date );
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/datereflection/JodaTimeExamples.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import org.joda.time.DateTime;
4 | import org.joda.time.format.DateTimeFormat;
5 | import org.joda.time.format.DateTimeFormatter;
6 |
7 | public class JodaTimeExamples {
8 | public static void main( String[] args ) {
9 |
10 | DateTime dt = new DateTime( ); // Joda Date
11 | System.out.println( "Date:" + dt.toDate( ) ); // Java Date
12 |
13 | int month = dt.getMonthOfYear( );
14 | System.out.println( "MonthOfYear: " + month );
15 |
16 | DateTime.Property pDoW = dt.dayOfWeek( );// Monday:1 to Sunday:7
17 | System.out.println( "dayOfWeek: " + pDoW.getAsText( ) ); // print:Monday/Tuesday
18 |
19 | System.out.println( "getDayOfMonth: " + dt.getDayOfMonth( ) );
20 | int maxDay = dt.dayOfMonth( ).getMaximumValue( );
21 | System.out.println( "Last day of this month: " + maxDay + " day" );
22 |
23 | boolean leapYear = dt.yearOfEra( ).isLeap( );
24 | System.out.println( "Leap Year: " + leapYear );
25 |
26 | DateTime datePlus20 = dt.plusDays( 20 );
27 | DateTimeFormatter formattedDate = DateTimeFormat.forPattern( "dd/MM/yyyy" );
28 | System.out.println( dt.toString( formattedDate ) + " + 20 day = " + datePlus20.toString( formattedDate ) );
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/datereflection/ReflectionClassName.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | public class ReflectionClassName {
4 | public static void main( String[] args ) throws ClassNotFoundException {
5 |
6 | Class c = Class.forName( "java.util.Date" );
7 | System.out.println( c.getName( ) );
8 |
9 | Integer intObj = new Integer( "25" );
10 | Class cInt = intObj.getClass( );
11 | System.out.println( cInt.getName( ) );
12 |
13 | Class c3 = String.class;
14 | System.out.println( c3.getName( ) );
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/datereflection/SimpleDateFormatTest.java:
--------------------------------------------------------------------------------
1 | package datereflection;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | public class SimpleDateFormatTest {
7 | public static void main( String[] args ) {
8 |
9 | for ( ;; ) {// Infinite Loop
10 |
11 | Date date = new Date( );
12 |
13 | SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy" );
14 | String strDate = formatter.format( date );
15 | System.out.println( "DateFormat: MM/dd/yyyy : " + strDate );
16 |
17 | formatter = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" );
18 | strDate = formatter.format( date );
19 | System.out.println( "DateFormat: dd-M-yyyy hh:mm:ss : " + strDate );
20 |
21 | formatter = new SimpleDateFormat( "dd MMMM yyyy" );
22 | strDate = formatter.format( date );
23 | System.out.println( "DateFormat: dd MMMM yyyy : " + strDate );
24 |
25 | try { // Only continue loop after 2 second
26 | Thread.sleep( 2000 );
27 | System.out.println( );
28 | } catch ( InterruptedException e ) {
29 | e.printStackTrace( );
30 | }
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/dt/AssignmentTest.java:
--------------------------------------------------------------------------------
1 | package dt;
2 |
3 | public class AssignmentTest {
4 | public static void main( String[] args ) {
5 | int x = 10;
6 | if ( x > 0 && x == 10 ) {
7 | System.out.println( "x is greate then 0: X= " + x );
8 | }
9 |
10 | if ( x > 0 || x == 50 ) {
11 | System.out.println( "x is greate then 0" );
12 | }
13 |
14 | int y = 50;
15 | y = y + 10; // 60
16 | System.out.println( y );
17 | y += 10; // 70
18 | System.out.println( y );
19 |
20 | AssignmentTest obj = new AssignmentTest( );
21 | obj.show( );
22 |
23 | }
24 |
25 | public void show( ) {
26 | double sin30 = Math.sin( Math.PI / 6 );
27 | System.out.println( sin30 );
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/dt/Light.java:
--------------------------------------------------------------------------------
1 | package dt;
2 |
3 | public class Light {
4 | public static void main( String[] args ) {
5 | int lightSpeed;
6 | long days;
7 | long seconds;
8 | long distance;
9 |
10 | lightSpeed = 186000;
11 | days = 1000;
12 |
13 | seconds = days * 24 * 60 * 60;
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/dt/Scope.java:
--------------------------------------------------------------------------------
1 | package dt;
2 |
3 | public class Scope {
4 |
5 | public static void main( String[] args ) {
6 |
7 | int x; // known to all code within main
8 |
9 | x = 10;
10 | if ( x == 10 ) { // start new scope
11 |
12 | int y = 20; // known only to this block
13 |
14 | // x and y both known here.
15 | System.out.println( "x and y: " + x + " " + y );
16 |
17 | x = y * 2;
18 | }
19 |
20 | // y = 100; /*ERROR: y is not known here */
21 |
22 | // x is still known here.
23 | System.out.println( "x is: " + x );
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/exhand/CompileTimeException.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileNotFoundException;
5 |
6 | public class CompileTimeException {
7 | public static void main( String[] args ) {
8 |
9 | try {
10 | FileInputStream fi = new FileInputStream( "C:\\te234234mp.text" );
11 | } catch ( FileNotFoundException e ) {
12 | // System.err.println( e.getLocalizedMessage( ) );
13 | e.printStackTrace( );
14 | }
15 |
16 | System.out.println( "You can continue here." );
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/exhand/CustomExceptionTest.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CustomExceptionTest {
6 | public static void main( String[] args ) {
7 |
8 | Scanner sc = null;
9 |
10 | CustomExceptionTest obj = new CustomExceptionTest( );
11 | try {
12 |
13 | sc = new Scanner( System.in );
14 |
15 | System.out.println( "Enter your Age: " );
16 | int age = sc.nextInt( );
17 |
18 | obj.checkEligibleAgeForVote( age );
19 | } catch ( InvalidAgeException e ) {
20 | e.printStackTrace( );
21 |
22 | } finally {
23 | sc.close( );
24 | }
25 |
26 | // Scanner sc = null;
27 | // try {
28 | // System.out.print( "Enter your Age: " );
29 | // sc = new Scanner( System.in );
30 | //
31 | // CustomExceptionTest obj = new CustomExceptionTest( );
32 | // int age = sc.nextInt( );
33 | // obj.checkEligibleAgeForVote( age );
34 | //
35 | // } catch ( InvalidAgeException e ) {
36 | // e.printStackTrace( );
37 | //
38 | // } finally {
39 | // sc.close( );
40 | // System.out.println( "Resource Closed." );
41 | // }
42 | }
43 |
44 | public void checkEligibleAgeForVote( int age ) throws InvalidAgeException {
45 |
46 | if ( age < 18 ) {
47 | throw new InvalidAgeException( "You are not eligible to vote. Age: " + age );
48 |
49 | } else {
50 | System.out.println( "You can vote. Age: " + age );
51 | }
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/exhand/FinallyTest.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.util.Scanner;
4 |
5 | public class FinallyTest {
6 |
7 | public static void main( String[] args ) {
8 | FinallyTest obj = new FinallyTest( );
9 | obj.finallyCallTest( );
10 | }
11 |
12 | public void finallyCallTest( ) {
13 |
14 | Scanner sc = null;
15 | try {
16 | sc = new Scanner( System.in );
17 |
18 | System.out.println( "Input a int value." );
19 | int val = sc.nextInt( ); // Error if you input String value.
20 | System.out.println( val );
21 |
22 | } catch ( ClassCastException e ) {
23 | e.printStackTrace( );
24 |
25 | } finally { // Always executed.
26 | sc.close( );
27 | System.out.println( "Scanner resource Closed." );
28 | }
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/exhand/InvalidAgeException.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class InvalidAgeException extends Exception {
4 | InvalidAgeException( String s ) {
5 | super( s );
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/exhand/MultiCatchException.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileNotFoundException;
5 |
6 | public class MultiCatchException {
7 | public static void main( String[] args ) {
8 | try {
9 |
10 | int val = 45 / 0; // ERROR: So below lines in try block will be not executed.
11 | FileInputStream fi = new FileInputStream( "C:\\temp.text" );
12 |
13 | } catch ( ArithmeticException e ) {
14 | System.out.println( "ArithmeticException" );
15 | e.printStackTrace( );
16 |
17 | } catch ( FileNotFoundException e ) {
18 | System.out.println( "FileNotFoundException" );
19 | e.printStackTrace( );
20 |
21 | } catch ( Exception e ) {
22 | System.out.println( "Exception is called." );
23 | e.printStackTrace( );
24 |
25 | }
26 |
27 | System.out.println( "Continue program here!" );
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/exhand/MultiTryCatch.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class MultiTryCatch {
4 |
5 | public static void main( String[] args ) {
6 |
7 | try {
8 |
9 | MultiTryCatch obj = new MultiTryCatch( );
10 | int divideResult = 0;
11 |
12 | try {
13 | divideResult = obj.divide( 10, 0 ); //Error:divide by 0
14 | } catch ( ArithmeticException e ) {
15 | System.out.println( "ERROR: 10/0" );
16 | e.printStackTrace( );
17 | }
18 |
19 | try {
20 | int[] arr = new int[ 5 ];
21 | arr[ 10 ] = 50; //Error: index is 10, array size is only 5
22 | } catch ( ArrayIndexOutOfBoundsException e ) {
23 | System.out.println( "ERROR: Array Size: 5, item insert index:10" );
24 | e.printStackTrace( );
25 | }
26 |
27 | System.out.println( divideResult );
28 |
29 | String str = null;
30 | System.out.println( str.length( ) ); //Error: method call in null value
31 |
32 | } catch ( Exception e ) {
33 | System.out.println( "Exception Root." );
34 | e.printStackTrace( );
35 | }
36 |
37 | }
38 |
39 | public int divide( int a, int b ) {
40 | return a / b;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/exhand/PgmWithoutException.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class PgmWithoutException {
4 |
5 | public static void main( String[] args ) {
6 | int no1 = 100;
7 | int no2 = 0;
8 |
9 | try {
10 | int value = no1 / no2;
11 | System.out.println( value );
12 | } catch ( ArithmeticException e ) {
13 | e.printStackTrace( );
14 | }
15 |
16 | double deposit = no1 + 1000.0;
17 | System.out.println( "Deposit Amont: " + deposit );
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/exhand/RuntimeExceptionAutoPropagate.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class RuntimeExceptionAutoPropagate {
4 |
5 | public static void main( String[] args ) {
6 |
7 | RuntimeExceptionAutoPropagate obj = new RuntimeExceptionAutoPropagate( );
8 | obj.method1( );
9 |
10 | System.out.println( "Write your logic here..." );
11 | }
12 |
13 | public void method1( ) {
14 | try {
15 | method2( );
16 | } catch ( ArithmeticException e ) {
17 | System.out.println( "****ArithmeticException handled. ****" );
18 | e.printStackTrace( );
19 | }
20 | }
21 |
22 | public void method2( ) {
23 | method3( );
24 | }
25 |
26 | public void method3( ) {
27 | int a = 75 / 0; // Runtime(Unchecked) Exception
28 | // It propagate error to caller method.
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/exhand/ThrowCheckExceptionCompileError.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileNotFoundException;
5 | import java.io.IOException;
6 |
7 | public class ThrowCheckExceptionCompileError {
8 | public static void main( String[] args ) {
9 |
10 | ThrowCheckExceptionCompileError obj = new ThrowCheckExceptionCompileError( );
11 | try {
12 | obj.method1( );
13 |
14 | } catch ( FileNotFoundException e ) {
15 | e.printStackTrace( );
16 |
17 | } catch ( IOException e ) {
18 | e.printStackTrace( );
19 | }
20 |
21 | System.out.println( "Write your logic here..." );
22 | }
23 |
24 | public void method1( ) throws FileNotFoundException, IOException {
25 | method2( );
26 | method3( );
27 | }
28 |
29 | public void method2( ) throws IOException {
30 | FileInputStream ios = new FileInputStream( "C:\\t44mp.txt" );
31 | }
32 |
33 | public void method3( ) throws FileNotFoundException {
34 | FileInputStream ios = new FileInputStream( "C:\\te243234mp.txt" );
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/exhand/ThrowExampleWithTryCatch.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class ThrowExampleWithTryCatch {
4 | public static void main( String[] args ) {
5 |
6 | try {
7 | validateAge( 17 ); // Exception came here.
8 |
9 | } catch ( ArithmeticException e ) {
10 | e.printStackTrace( );
11 | }
12 |
13 | System.out.println( "Your Other logic..." );
14 | }
15 |
16 | public static void validateAge( int age ) {
17 | if ( age < 18 ) {
18 | throw new ArithmeticException( "Age: " + age + " is not valid." );
19 | } else {
20 | System.out.println( "You are eligible to vote." );
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/exhand/ThrowTest.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | public class ThrowTest {
4 |
5 | public static void main( String[] args ) {
6 |
7 | validateAge( 17 ); // Exception came here.
8 |
9 | System.out.println( "Your Other logic..." );
10 | }
11 |
12 | public static void validateAge( int age ) {
13 | if ( age < 18 ) {
14 | throw new ArithmeticException( "Age: " + age + " is not valid." );
15 | } else {
16 | System.out.println( "You are eligible to vote." );
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/exhand/ThrowsUncheckPropagation.java:
--------------------------------------------------------------------------------
1 | package exhand;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileNotFoundException;
5 |
6 | public class ThrowsUncheckPropagation {
7 |
8 | public static void main( String[] args ) {
9 |
10 | ThrowsUncheckPropagation obj = new ThrowsUncheckPropagation( );
11 | obj.method1( );
12 |
13 | System.out.println( "Write your logic here..." );
14 | }
15 |
16 | public void method1( ) {
17 |
18 | try {
19 | method2( );
20 |
21 | } catch ( FileNotFoundException e ) {
22 | System.out.println( "****FileNotFoundException is handled. ****" );
23 | e.printStackTrace( );
24 | }
25 | }
26 |
27 | public void method2( ) throws FileNotFoundException {
28 | method3( );
29 | }
30 |
31 | public void method3( ) throws FileNotFoundException {
32 | FileInputStream ios = new FileInputStream( "C:\\temp.txt" );
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/innerclass/ActionEvent.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public abstract class ActionEvent {
4 | public abstract void actionPerformed( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/innerclass/ActionListener.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public interface ActionListener {
4 | void actionPerformed( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/innerclass/AnnInnAbstClass.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class AnnInnAbstClass {
4 |
5 | public static void main( String[] args ) {
6 |
7 | // Shyam is eating without annyonymous
8 | Person sEat = new ShyamEat( );
9 | sEat.eat( );
10 |
11 | // Ram is eating :annyonymous
12 | Person ramEat = new Person( ) {
13 | @Override
14 | void eat( ) {
15 | System.out.println( "Ram is eating..." );
16 | }
17 | };
18 | ramEat.eat( );
19 |
20 | // Hari eat: annyonymous
21 | Person hariEat = new Person( ) {
22 | @Override
23 | void eat( ) {
24 | System.out.println( "Hari is eating.." );
25 | }
26 | };
27 | hariEat.eat( );
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/innerclass/AnnonymousInterfaceImpl.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class AnnonymousInterfaceImpl {
4 | public static void main( String[] args ) {
5 |
6 | Eatable e = new Eatable( ) {
7 | @Override
8 | public void eat( ) {
9 | System.out.println( "I am eating.." );
10 | }
11 | };
12 | e.eat( );
13 |
14 | new Eatable( ) {
15 | @Override
16 | public void eat( ) {
17 | System.out.println( "No Object Name: Eating.." );
18 | }
19 | }.eat( );
20 |
21 | }
22 | }
23 |
24 | interface Eatable {
25 | void eat( );
26 | }
27 |
--------------------------------------------------------------------------------
/src/innerclass/ButtonClick.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class ButtonClick {
4 |
5 | public void addActionEvent( ActionEvent e ) {
6 | e.actionPerformed( );
7 | }
8 |
9 | public static void main( String[] args ) {
10 | button1Clicked( );
11 | button2Clicked( );
12 | }
13 |
14 | public static void button1Clicked( ) {
15 | // No1:way
16 | ActionEvent aEvent = new ActionEvent( ) {
17 | @Override
18 | public void actionPerformed( ) {
19 | System.out.println( "HELLO From actionPerformed: 1" );
20 | }
21 | };
22 | ButtonClick button2 = new ButtonClick( );
23 | button2.addActionEvent( aEvent );
24 |
25 | }
26 |
27 | public static void button2Clicked( ) {
28 | // NO2:way
29 | ButtonClick button = new ButtonClick( );
30 | button.addActionEvent( new ActionEvent( ) {
31 | @Override
32 | public void actionPerformed( ) {
33 | System.out.println( "HELLO From actionPerformed: 2" );
34 | }
35 | } );
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/innerclass/ButtonClickActionListener.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class ButtonClickActionListener {
4 | public void addActionListener( ActionListener e ) {
5 | e.actionPerformed( );
6 | }
7 |
8 | public static void main( String[] args ) {
9 | button1Clicked( );
10 | button2Clicked( );
11 | }
12 |
13 | public static void button1Clicked( ) {
14 | // No1:way
15 | ActionListener aEvent = new ActionListener( ) {
16 | @Override
17 | public void actionPerformed( ) {
18 | System.out.println( "HELLO From actionPerformed: 1" );
19 | }
20 | };
21 | ButtonClickActionListener button2 = new ButtonClickActionListener( );
22 | button2.addActionListener( aEvent );
23 |
24 | }
25 |
26 | public static void button2Clicked( ) {
27 | // NO2:way
28 | ButtonClickActionListener button = new ButtonClickActionListener( );
29 | button.addActionListener( new ActionListener( ) {
30 | @Override
31 | public void actionPerformed( ) {
32 | System.out.println( "HELLO From actionPerformed: 2" );
33 | }
34 | } );
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/innerclass/EmployeeOuter.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class EmployeeOuter {
4 | private int id;
5 | private String name;
6 |
7 | public EmployeeOuter( int id, String name ) {
8 | this.id = id;
9 | this.name = name;
10 | }
11 |
12 | public static void main( String[] args ) {
13 | EmployeeOuter employee = new EmployeeOuter( 100, "Bill Gate" );
14 |
15 | AddressInner address = employee.new AddressInner( "New York City", "New York", "00501", "USA" );
16 | address.showProperties( );
17 | }
18 |
19 | private class AddressInner {
20 | private String city;
21 | private String state;
22 | private String zip;
23 | private String country;
24 |
25 | public AddressInner( String city, String state, String zip, String country ) {
26 | this.city = city;
27 | this.state = state;
28 | this.zip = zip;
29 | this.country = country;
30 | }
31 |
32 | private void showProperties( ) {
33 | // Inner class can access outer class all data members including private.
34 | System.out.println( id );
35 | System.out.println( name );
36 |
37 | System.out.println( city );
38 | System.out.println( state );
39 | System.out.println( zip );
40 | System.out.println( country );
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/innerclass/Person.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public abstract class Person {
4 | abstract void eat( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/innerclass/ShyamEat.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class ShyamEat extends Person {
4 |
5 | @Override
6 | void eat( ) {
7 | System.out.println( "Shyam is eating..." );
8 |
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/innerclass/StaticInnerClassWithStaticMethod.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class StaticInnerClassWithStaticMethod {
4 |
5 | static int count = 100;
6 |
7 | public static void main( String[] args ) {
8 | StaticInnerClassWithStaticMethod.StaticInnerI.increaseAndDisplay( );
9 | StaticInnerClassWithStaticMethod.StaticInnerI.increaseAndDisplay( );
10 | }
11 |
12 | private static class StaticInnerI {
13 | public static void increaseAndDisplay( ) {
14 | System.out.println( ++count );
15 | }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/innerclass/StaticInnerO.java:
--------------------------------------------------------------------------------
1 | package innerclass;
2 |
3 | public class StaticInnerO {
4 | private static int count = 100;
5 |
6 | public static void main( String[] args ) {
7 | StaticInnerO.StaticInnerI inn = new StaticInnerO.StaticInnerI( );
8 | inn.increaseAndDisplay( );
9 | inn.increaseAndDisplay( );
10 | }
11 |
12 | public static class StaticInnerI {
13 | public void increaseAndDisplay( ) {
14 | System.out.println( ++count );
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/io/BufferedInputStreamExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.FileInputStream;
5 |
6 | public class BufferedInputStreamExample {
7 | public static void main( String args[] ) {
8 | try {
9 |
10 | FileInputStream fin = new FileInputStream( "D:\\bstud.txt" );
11 | BufferedInputStream bin = new BufferedInputStream( fin );
12 |
13 | int i;
14 | while ( ( i = bin.read( ) ) != -1 ) {
15 | System.out.println( ( char ) i );
16 | }
17 |
18 | bin.close( );
19 | fin.close( );
20 |
21 | } catch ( Exception e ) {
22 | e.printStackTrace( );
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/io/BufferedOutputStreamExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.BufferedOutputStream;
4 | import java.io.FileOutputStream;
5 | import java.util.Scanner;
6 |
7 | public class BufferedOutputStreamExample {
8 | public static void main( String[] args ) {
9 | try {
10 | FileOutputStream fout = new FileOutputStream( "D:\\bstud.txt" );
11 | BufferedOutputStream bout = new BufferedOutputStream( fout );
12 |
13 | System.out.println( "Type Text:" );
14 | Scanner sc = new Scanner( System.in );
15 | String str = sc.nextLine( );
16 |
17 | // String str = "Java developers of Nepal.\nRunning Chapter: File Handling, BufferedOutputStream";
18 | byte[] b = str.getBytes( );
19 |
20 | bout.write( b );
21 | bout.flush( );
22 |
23 | bout.close( );
24 | fout.close( );
25 |
26 | System.out.println( "Information is added in the file." );
27 |
28 | } catch ( Exception e ) {
29 | e.printStackTrace( );
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/io/BufferedReaderInputStreamReader.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 |
7 | public class BufferedReaderInputStreamReader {
8 | public static void main( String[] args ) {
9 |
10 | /**
11 | * InputStreamReader ir = new InputStreamReader( System.in );
12 | * BufferedReader br = new BufferedReader(ir);
13 | */
14 | BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
15 |
16 | try {
17 | System.out.println( "Write anything." );
18 |
19 | String name = br.readLine( );
20 |
21 | System.out.println( "You Wrote: " + name );
22 |
23 | } catch ( IOException e ) {
24 | e.printStackTrace( );
25 | }
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/io/DeserializeUser.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.io.FileNotFoundException;
6 | import java.io.IOException;
7 | import java.io.ObjectInputStream;
8 |
9 | public class DeserializeUser {
10 | public static void main( String[] args ) throws FileNotFoundException, IOException, ClassNotFoundException {
11 |
12 | ObjectInputStream io = new ObjectInputStream( new FileInputStream( new File( "D:\\user.txt" ) ) );
13 |
14 | User user = ( User ) io.readObject( );
15 | io.close( );
16 |
17 | System.out.println( user );
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/io/FileExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.File;
4 |
5 | public class FileExample {
6 |
7 | public static void main( String[] args ) {
8 |
9 | /* Instantiate File */
10 | File file = new File( "D:\\ab.txt" );
11 |
12 | // Check if above path is Directory?
13 | boolean isDirectory = file.isDirectory( );
14 | System.out.println( "ab.txt is directory: " + isDirectory );
15 |
16 | /* File exist of not */
17 | boolean isFileExits = file.exists( );
18 | System.out.println( "File is exist: " + isFileExits );
19 |
20 | /* Create Directory */
21 | File fDir = new File( "D:\\JavaStud" );
22 | boolean dirCreated = fDir.mkdir( );
23 | if ( dirCreated ) {
24 | System.out.println( "JavaStud is successfully created." );
25 | }
26 |
27 | /* List name of all files */
28 | String[] fileNames = fDir.list( );
29 | for ( String fileName: fileNames ) {
30 | System.out.println( fileName );
31 | }
32 |
33 | /* List of Files */
34 | File[] files = fDir.listFiles( );
35 | for ( File file2: files ) {
36 | System.out.println( file2.getAbsolutePath( ) + " " + file2.isDirectory( ) );
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/io/FileInputStreamExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.FileInputStream;
4 |
5 | public class FileInputStreamExample {
6 | public static void main( String[] args ) {
7 |
8 | try {
9 | FileInputStream fin = new FileInputStream( "D:\\stud.txt" );
10 | int i = 0;
11 | while ( ( i = fin.read( ) ) != -1 ) {
12 | System.out.print( ( char ) i );
13 | }
14 | fin.close( );
15 |
16 | } catch ( Exception e ) {
17 | e.printStackTrace( );
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/io/FileOutputStreamExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.FileOutputStream;
5 | import java.io.IOException;
6 |
7 | public class FileOutputStreamExample {
8 | public static void main( String[] args ) {
9 | try {
10 | FileOutputStream fout = new FileOutputStream( "D:\\stud.txt" );
11 | String str = "Java developers of Nepal.\nRunning Chapter: File Handling.";
12 | byte[] b = str.getBytes( );
13 | fout.write( b );
14 | fout.close( );
15 | System.out.println( "Information is added in the file." );
16 | } catch ( FileNotFoundException e ) {
17 | e.printStackTrace( );
18 | } catch ( IOException e ) {
19 | e.printStackTrace( );
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/io/FileReaderExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.FileReader;
4 |
5 | public class FileReaderExample {
6 | public static void main( String args[] ) throws Exception {
7 |
8 | FileReader fr = new FileReader( "D:\\fstud.txt" );
9 | int i;
10 |
11 | while ( ( i = fr.read( ) ) != -1 ) {
12 |
13 | System.out.print( ( char ) i );
14 | }
15 |
16 | fr.close( );
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/io/FileStreamReadOneWriteToOther.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 |
6 | /**
7 | * Copy content of one file to another.
8 | * @author ojhay
9 | */
10 | public class FileStreamReadOneWriteToOther {
11 | public static void main( String[] args ) {
12 |
13 | try {
14 | FileInputStream fin = new FileInputStream( "D:\\stud.txt" );
15 | FileOutputStream fout = new FileOutputStream( "D:\\stud2.txt" );
16 | int i = 0;
17 | while ( ( i = fin.read( ) ) != -1 ) {
18 | System.out.print( ( char ) i );
19 | fout.write( ( char ) i );
20 | }
21 | fin.close( );
22 | fout.close( );
23 |
24 | } catch ( Exception e ) {
25 | e.printStackTrace( );
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/io/FileWriterExample.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.FileWriter;
4 |
5 | public class FileWriterExample {
6 | public static void main( String args[] ) {
7 | try {
8 |
9 | FileWriter fw = new FileWriter( "D:\\fstud.txt" );
10 |
11 | fw.write( "Java developers of नेपाल.\nRunning Chapter: File Handling, FileWriter." );
12 |
13 | fw.close( );
14 |
15 | } catch ( Exception e ) {
16 | e.printStackTrace( );
17 | }
18 | System.out.println( "Information is added in the file." );
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/io/ReadCsv.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileNotFoundException;
6 | import java.io.FileReader;
7 | import java.io.IOException;
8 |
9 | public class ReadCsv {
10 | public static void main( String[] args ) {
11 | BufferedReader br = null;
12 | try {
13 | br = new BufferedReader( new FileReader( new File( "D:\\student.csv" ) ) );
14 | String row;
15 | System.out.println( "ID:\tName\tADdress" );
16 | while ( ( row = br.readLine( ) ) != null ) {
17 | String[] cols = row.split( "," );
18 | System.out.println( cols[ 0 ] + "\t" + cols[ 1 ] + "\t" + cols[ 2 ] );
19 | }
20 | } catch ( FileNotFoundException e ) {
21 | e.printStackTrace( );
22 |
23 | } catch ( IOException e ) {
24 | e.printStackTrace( );
25 |
26 | } finally {
27 | if ( br != null ) {
28 | try {
29 | br.close( );
30 | } catch ( IOException e ) {
31 | e.printStackTrace( );
32 | }
33 | }
34 | }
35 |
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/io/ScannerConsoleInput.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ScannerConsoleInput {
6 | public static void main( String[] args ) {
7 |
8 | Scanner sc = new Scanner( System.in );
9 | System.out.println( "Student Detail." );
10 |
11 | System.out.print( "Name: " );
12 | String name = sc.next( );
13 |
14 | System.out.print( "\nRollNo: " );
15 | int rollNo = sc.nextInt( );
16 |
17 | System.out.print( "\nAddress: " );
18 | String address = sc.next( );
19 |
20 | System.out.println( "\nYour Name: " + name + " RollNo: " + rollNo + " Address: " + address );
21 | sc.close( );
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/io/ScannerDelimated.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ScannerDelimated {
6 | public static void main( String[] args ) {
7 | String input = "5 teacher and 200 students are in Velley school.";
8 | Scanner s = new Scanner( input ).useDelimiter( "\\s" );
9 | System.out.println( s.nextInt( ) ); // 5
10 | System.out.println( s.next( ) ); // teacher
11 | s.next( );// and
12 | System.out.println( s.nextInt( ) );// 200
13 | System.out.println( s.next( ) ); // students
14 | s.close( );
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/io/ScannerEx.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ScannerEx {
6 |
7 | public static void main(String[] args) {
8 | System.out.print("Write Something in console window: ");
9 | Scanner sc = new Scanner(System.in);
10 |
11 | String inputText = sc.nextLine();//take input value
12 | System.out.println(inputText);
13 |
14 | int a = sc.nextInt();
15 | System.err.println(a);
16 |
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/io/SerializationUser.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.io.FileOutputStream;
6 | import java.io.IOException;
7 | import java.io.ObjectOutputStream;
8 |
9 | public class SerializationUser {
10 | public static void main( String[] args ) throws FileNotFoundException, IOException {
11 |
12 | User user = new User( 1, "Harry" );
13 |
14 | File file = new File( "D:\\user.txt" );
15 | FileOutputStream fos = new FileOutputStream( file );
16 |
17 | ObjectOutputStream out = new ObjectOutputStream( fos );
18 |
19 | out.writeObject( user );
20 | out.flush( );
21 | out.close( );
22 |
23 | System.out.println( "User object is saved in file " + file.getAbsolutePath( ) );
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/io/TryWithResource.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileNotFoundException;
6 | import java.io.FileReader;
7 | import java.io.IOException;
8 |
9 | public class TryWithResource {
10 | public static void main( String[] args ) {
11 | try (BufferedReader br = new BufferedReader( new FileReader( new File( "D:\\student.csv" ) ) )) {
12 | String row;
13 | System.out.println( "ID:\tName\tADdress" );
14 | while ( ( row = br.readLine( ) ) != null ) {
15 | String[] cols = row.split( "," );
16 | System.out.println( cols[ 0 ] + "\t" + cols[ 1 ] + "\t" + cols[ 2 ] );
17 | }
18 | } catch ( FileNotFoundException e ) {
19 | e.printStackTrace( );
20 |
21 | } catch ( IOException e ) {
22 | e.printStackTrace( );
23 |
24 | }
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/io/User.java:
--------------------------------------------------------------------------------
1 | package io;
2 |
3 | import java.io.Serializable;
4 |
5 | public class User implements Serializable {
6 |
7 | private static final long serialVersionUID = -8986612101553037402L;
8 |
9 | private int id; // transient
10 |
11 | private transient String name;
12 |
13 | public User( int id, String name ) {
14 | this.id = id;
15 | this.name = name;
16 | }
17 |
18 | public int getId( ) {
19 | return id;
20 | }
21 |
22 | public void setId( int id ) {
23 | this.id = id;
24 | }
25 |
26 | public String getName( ) {
27 | return name;
28 | }
29 |
30 | public void setName( String name ) {
31 | this.name = name;
32 | }
33 |
34 | @Override
35 | public String toString( ) {
36 | return id + " " + name;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/jdbc/DeleteStatement.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.SQLException;
6 | import java.sql.Statement;
7 |
8 | //DELETE FROM `ncitdb`.`user` WHERE `id` = '2';
9 | public class DeleteStatement {
10 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
11 |
12 | // 1. Register driver.
13 | Class.forName("com.mysql.jdbc.Driver");
14 |
15 | // 2. Create Connection
16 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ncitdb", "root", "");
17 |
18 | // 3. Create Statement
19 | Statement stat = conn.createStatement();
20 |
21 | stat.executeUpdate("DELETE FROM user WHERE username = 'sushma'");
22 |
23 | System.out.println("Query Deleted: ");
24 |
25 | conn.close();
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/jdbc/InsertStatement.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.SQLException;
6 | import java.sql.Statement;
7 |
8 | public class InsertStatement {
9 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
10 |
11 | // 1. Register driver.
12 | Class.forName("com.mysql.jdbc.Driver");
13 |
14 | // 2. Create Connection
15 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ncitdb", "root", "");
16 |
17 | // 3. Create Statement
18 | Statement stat = conn.createStatement();
19 |
20 | stat.executeUpdate("INSERT INTO `user` (`username`, `password`) VALUES ('ncit', 'ncit@ktm')");
21 |
22 | System.out.println("Data Inserted.: ");
23 |
24 | conn.close();
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/jdbc/JDBConnection.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.ResultSet;
6 | import java.sql.SQLException;
7 | import java.sql.Statement;
8 |
9 | public class JDBConnection {
10 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
11 |
12 | // 1. Register driver.
13 | Class.forName("com.mysql.jdbc.Driver");
14 |
15 | // 2.Connect to the Database: URL:database Path, username, password.
16 | // here studentdb is database name, root is username and password
17 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ncitdb", "root", "");
18 |
19 | //3. Create Statement
20 | Statement stat = conn.createStatement();
21 |
22 | //4. Execute query and fetch record.
23 | ResultSet rs = stat.executeQuery("SELECT * FROM user");
24 |
25 | // 5. Fetch the result.
26 | while(rs.next()){
27 | //rs = one row of database
28 | long id = rs.getLong("id");
29 | String user = rs.getString("username");
30 | String pass = rs.getString("password");
31 | System.out.println(id + "\t" + user + "\t" + pass);
32 | }
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/jdbc/JdbcConnectionTest.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.ResultSet;
6 | import java.sql.SQLException;
7 | import java.sql.Statement;
8 |
9 | public class JdbcConnectionTest {
10 | public static void main(String[] args) {
11 |
12 | Connection con = null;
13 | try {
14 | // 1. Load Driver
15 | Class.forName("com.mysql.jdbc.Driver");
16 |
17 | // 2.Connect to the Database: URL:database Path, username, password.
18 | // here studentdb is database name, root is username and password
19 | con = DriverManager.getConnection(
20 | "jdbc:mysql://localhost:3306/studentdb", "root", "");
21 |
22 | // 3. Create Statement
23 | Statement stmt = con.createStatement();
24 |
25 | // 4. Execute Query
26 | ResultSet rs = stmt.executeQuery("select * from student");
27 |
28 | // 5. Fetch the result.
29 | while (rs.next()) {// Loop Each Row
30 | // Fetch columns
31 | // System.out.println(rs.getInt(1) + " " + rs.getString(2));
32 | System.out.println(rs.getInt("id") + " " + rs.getString("name"));
33 | }
34 |
35 | } catch (Exception e) {
36 | e.printStackTrace();
37 |
38 | } finally {
39 | // 6. Close connection.
40 | try {
41 | con.close();
42 | } catch (SQLException e) {
43 | e.printStackTrace();
44 | }
45 | }
46 |
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/jdbc/MysqlConnection.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.SQLException;
6 |
7 | public class MysqlConnection {
8 |
9 | public static Connection getConnection( ) throws ClassNotFoundException, SQLException {
10 | Class.forName( "com.mysql.jdbc.Driver" );
11 | Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", "root", "" );
12 | return con;
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/jdbc/UpdateStatment.java:
--------------------------------------------------------------------------------
1 | package jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.SQLException;
6 | import java.sql.Statement;
7 |
8 | //UPDATE `ncitdb`.`user` SET `password` = 'admin@123' WHERE `id` = '1';
9 | public class UpdateStatment {
10 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
11 |
12 | // 1. Register driver.
13 | Class.forName("com.mysql.jdbc.Driver");
14 |
15 | // 2. Create Connection
16 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ncitdb", "root", "");
17 |
18 | // 3. Create Statement
19 | Statement stat = conn.createStatement();
20 |
21 | stat.executeUpdate("UPDATE `user` SET `password` = 'admin#123' WHERE `id` = '1'");
22 |
23 | System.out.println("Query updated: ");
24 |
25 | conn.close();
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/mthread/FacebookLike.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class FacebookLike {
4 |
5 | private Integer likes = 0;
6 |
7 | /* set current Page likes */
8 | public FacebookLike( Integer likes ) {
9 | this.likes = likes;
10 | }
11 |
12 | /* Synchronized method call solve problem of Multiple-thread(user) */
13 | public void plusOne( ) {
14 |
15 | // Increase like by 1.
16 | likes++;
17 |
18 | // Thread.current thread get current thread object just like this keyword.
19 | System.out.println( Thread.currentThread( ).getName( ) + " Likes: " + likes );
20 |
21 | // Thread.sleep halt running thread for 1/10 second time.
22 | try {
23 | Thread.sleep( 100 );
24 | } catch ( InterruptedException e ) {
25 | e.printStackTrace( );
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/mthread/RunnableInterface.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class RunnableInterface implements Runnable {
4 |
5 | @Override
6 | public void run( ) {
7 | System.out.println( "Thread: Runnable> run()" );
8 | }
9 |
10 | public static void main( String[] args ) {
11 | RunnableInterface runnable = new RunnableInterface( );
12 | Thread t = new Thread( runnable );
13 | t.start( );
14 | System.out.println( "Thread: RunnableInterface> Main()" );
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/mthread/ShutdownHookExample.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class ShutdownHookExample {
4 | public static void main( String[] args ) {
5 |
6 | Thread t1 = new Thread( ) {
7 | @Override
8 | public void run( ) {
9 | System.out.println( "Shut down hook run this method. " );
10 | }
11 | };
12 |
13 | Runtime r = Runtime.getRuntime( );
14 | r.addShutdownHook( t1 );
15 |
16 | try {
17 | System.out.println( "Press ctrl+c in command window to exit program." );
18 | Thread.sleep( 3000 );
19 | } catch ( InterruptedException e ) {
20 | e.printStackTrace( );
21 | }
22 |
23 | System.out.println( "Calling System.exit()..." );
24 | System.exit( 0 ); // exit program in the middle
25 |
26 | System.out.println( "After 3 second.." );
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/mthread/SynchronizeFbLike.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class SynchronizeFbLike {
4 |
5 | public static void main( String[] args ) {
6 |
7 | /* Facebook Page: Everest, Current Likes: 500 */
8 | final FacebookLike everestFbPagePiclike = new FacebookLike( 500 );
9 |
10 | Thread user1 = new Thread( ) {
11 | public void run( ) {
12 | everestFbPagePiclike.plusOne( );
13 | }
14 | };
15 | Thread user2 = new Thread( ) {
16 | public void run( ) {
17 | everestFbPagePiclike.plusOne( );
18 | }
19 | };
20 | Thread user3 = new Thread( ) {
21 | public void run( ) {
22 | everestFbPagePiclike.plusOne( );
23 | }
24 | };
25 | Thread user4 = new Thread( ) {
26 | public void run( ) {
27 | everestFbPagePiclike.plusOne( );
28 | }
29 | };
30 |
31 | /* User1,2,3,4 hit like button in Everest Facebook Page */
32 | user1.start( );
33 | user2.start( );
34 | user3.start( );
35 | user4.start( );
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/mthread/ThreadClass.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class ThreadClass extends Thread {
4 |
5 | @Override
6 | public void run( ) {
7 | System.out.println( "ThreadClass>Run> HELLO" );
8 | }
9 |
10 | public static void main( String[] args ) {
11 | ThreadClass t = new ThreadClass( );
12 | t.start( );
13 | System.out.println( "ThreadClass: Main" );
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/mthread/ThreadJoinCalcTanx.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class ThreadJoinCalcTanx {
4 | private double sin45;
5 | private double cos45;
6 |
7 | public static void main( String[] args ) {
8 |
9 | final ThreadJoinCalcTanx obj = new ThreadJoinCalcTanx( );
10 |
11 | Runnable sin45r = new Runnable( ) {
12 | @Override
13 | public void run( ) {
14 | obj.sin45 = Math.sin( Math.PI / 4 );
15 | }
16 | };
17 |
18 | Thread sin45t = new Thread( sin45r );
19 | sin45t.start( );
20 |
21 | Runnable cos45r = new Runnable( ) {
22 | @Override
23 | public void run( ) {
24 | obj.cos45 = Math.cos( Math.PI / 4 );
25 | }
26 | };
27 |
28 | Thread cos45t = new Thread( cos45r );
29 | cos45t.start( );
30 |
31 | try {
32 | sin45t.join( );
33 | cos45t.join( );
34 | } catch ( InterruptedException e ) {
35 | e.printStackTrace( );
36 | }
37 |
38 | double tan45 = Math.round( obj.sin45 / obj.cos45 );
39 | System.out.println( "Tan45: " + tan45 ); // Nan:0.0 / 0.0;Infinity:5.0 / 0.0
40 |
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/mthread/ThreadJoinPrintValues.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class ThreadJoinPrintValues extends Thread {
4 | public void run( ) {
5 | for ( int i = 1; i <= 5; i++ ) {
6 |
7 | try {
8 | Thread.sleep( 500 );
9 | } catch ( Exception e ) {
10 | e.printStackTrace( );
11 | }
12 |
13 | System.out.println( i );
14 | }
15 | }
16 |
17 | public static void main( String args[] ) {
18 | ThreadJoinPrintValues t1 = new ThreadJoinPrintValues( );
19 | ThreadJoinPrintValues t2 = new ThreadJoinPrintValues( );
20 | ThreadJoinPrintValues t3 = new ThreadJoinPrintValues( );
21 | t1.start( );
22 |
23 | try {
24 | t1.join( ); // t1.join( 1500 );start t2 after 1500 sec
25 | } catch ( Exception e ) {
26 | e.printStackTrace( );
27 | }
28 |
29 | t2.start( );
30 | t3.start( );
31 |
32 | Thread t = new Thread( ) {
33 | public void run( ) {
34 | System.out.println( "Hello from thread." );
35 | }
36 | };
37 | t.start( );
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/mthread/ThreadsWithName.java:
--------------------------------------------------------------------------------
1 | package mthread;
2 |
3 | public class ThreadsWithName extends Thread {
4 |
5 | @Override
6 | public void run( ) {
7 | System.out.println( "run(): Thread Name: " + Thread.currentThread( ).getName( ) );
8 | try {
9 | Thread.sleep( 2000 );
10 | } catch ( InterruptedException e ) {
11 | e.printStackTrace( );
12 | }
13 | System.out.println( "Priority: " + Thread.currentThread( ).getPriority( ) +
14 | " IsDemon:" + Thread.currentThread( ).isDaemon( ) );
15 | }
16 |
17 | public static void main( String[] args ) {
18 | ThreadsWithName t = new ThreadsWithName( );
19 | t.setPriority( Thread.MAX_PRIORITY );
20 | t.setName( "Thread1" );
21 |
22 | ThreadsWithName t2 = new ThreadsWithName( );
23 | t2.setName( "Thread2" );
24 | t2.setPriority( 4 );
25 | t2.setDaemon( true );
26 |
27 | t.start( );
28 | t2.start( );
29 |
30 | System.out.println( "main(): Thread Name: " + Thread.currentThread( ).getName( ) );
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/oop/ChangeStaticValue.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class ChangeStaticValue {
4 | int rollno;
5 | String name;
6 | private static String college = "TRICHANDRA";
7 |
8 | public static void change( String collegeName ) {
9 | college = collegeName;
10 | }
11 |
12 | ChangeStaticValue( int rollno, String name ) {
13 | this.rollno = rollno;
14 | this.name = name;
15 | }
16 |
17 | void display( ) {
18 | System.out.println( rollno + " " + name + " " + college + "\n" );
19 | }
20 |
21 | public static void main( String args[] ) {
22 |
23 | ChangeStaticValue s1 = new ChangeStaticValue( 111, "Amrit" );
24 | s1.display( );
25 |
26 | ChangeStaticValue.college = "ASCOL";
27 | ChangeStaticValue s2 = new ChangeStaticValue( 222, "Devid" );
28 | ChangeStaticValue s3 = new ChangeStaticValue( 333, "Lewis" );
29 |
30 | s1.display( );
31 | s2.display( );
32 | s3.display( );
33 |
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/oop/CounterWithStatic.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class CounterWithStatic {
4 | static int count = 0;// will get memory only once and retain its value
5 |
6 | CounterWithStatic() {
7 | count++;
8 | }
9 |
10 | private void showCount() {
11 | System.out.println(count);
12 | }
13 |
14 | public static void main(String args[]) {
15 |
16 | new CounterWithStatic().showCount();
17 | new CounterWithStatic().showCount();
18 | new CounterWithStatic().showCount();
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/oop/CounterWithoutStatic.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class CounterWithoutStatic {
4 | int count = 0;// will get memory when instance is created
5 |
6 | CounterWithoutStatic() {
7 | count++;
8 | }
9 |
10 | private void showCount() {
11 | System.out.println(count);
12 | }
13 |
14 | public static void main(String args[]) {
15 |
16 | new CounterWithoutStatic().showCount();
17 | new CounterWithoutStatic().showCount();
18 | new CounterWithoutStatic().showCount();
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/oop/PassObjAsParam.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class PassObjAsParam {
4 |
5 | public static void main(String[] args) {
6 | Student s1 = new Student(1, 50);
7 | Student s2 = new Student(1, 50);
8 | Student s3 = new Student(2, 20);
9 | System.out.println("Object s1 == s2: "+ s1.equals(s2) );
10 | System.out.println("Object s1 == s3: "+ s1.equals(s3) );
11 | }
12 |
13 | }
14 |
15 | class Student {
16 | private int id = 10;
17 | private int rollNo = 30;
18 |
19 | public Student(int id, int rollNo) {
20 | this.id = id;
21 | this.rollNo = rollNo;
22 | }
23 |
24 | boolean equals(Student o) { //Object as a parameter.
25 | if (this.id == o.id && this.rollNo == o.rollNo) {
26 | return true;
27 | }
28 | return false;
29 | }
30 | }
--------------------------------------------------------------------------------
/src/oop/PrintThisKeyword.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class PrintThisKeyword {
4 |
5 | public void printThis() {
6 | System.out.println(this); // prints same reference ID
7 | }
8 |
9 | public static void main(String[] args) {
10 | PrintThisKeyword obj = new PrintThisKeyword();
11 | obj.printThis();
12 |
13 | System.out.println(obj); // prints the reference ID
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/StaticBlock.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class StaticBlock {
4 | private static String college;
5 |
6 | static {
7 | college = "ASCOL";
8 | System.out.println( "static block is invoked" );
9 | }
10 |
11 | public static void main( String args[] ) {
12 | System.out.println( "Hello from main" );
13 |
14 | System.out.println( college ); // ASCOL
15 |
16 | college = "BRITISH";
17 |
18 | System.out.println( college );
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/oop/StaticVariable.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | public class StaticVariable {
4 | int rollno;
5 | String name;
6 | static String college = "ITS";
7 |
8 | StaticVariable( int r, String n ) {
9 | rollno = r;
10 | name = n;
11 | }
12 |
13 | void display( ) {
14 | System.out.println( rollno + " " + name + " " + college + "\n" );
15 | }
16 |
17 | public static void main( String args[] ) {
18 |
19 | StaticVariable s1 = new StaticVariable( 111, "Karan" );
20 | StaticVariable s2 = new StaticVariable( 222, "Aryan" );
21 |
22 | s1.display( );
23 |
24 | StaticVariable.college = "ASCOL"; // s1.college = "ASCOL"
25 |
26 | s1.display( );
27 | s2.display( );
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/oop/VechicleTestMain.java:
--------------------------------------------------------------------------------
1 | package oop;
2 |
3 | import oop.inheritence.Vehicle;
4 |
5 | public class VechicleTestMain extends Vehicle {
6 | public static void main(String[] args) {
7 | VechicleTestMain v = new VechicleTestMain();
8 | System.out.println(v.speed);
9 | System.out.println(v.size);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/oop/abst/AbstractClassTest.java:
--------------------------------------------------------------------------------
1 | package oop.abst;
2 |
3 | public class AbstractClassTest {
4 | public static void main( String[] args ) {
5 |
6 | Bank nabil = new Nabil( );
7 | displayBankInfo( nabil );
8 |
9 | Bank nicAsia = new NicAsia( );
10 | displayBankInfo( nicAsia );
11 |
12 | Bank stdChrt = new StandChart( );
13 | displayBankInfo( stdChrt );
14 | }
15 |
16 | public static void displayBankInfo( Bank bank ) {
17 | System.out.println( "Bank Name: " + bank.getBankName( ) + " Rate: " + bank.getRate( ) );
18 | System.out.println( "Service Charge: " + bank.serviceChargeRate + " Dollar Ex Rate: " + bank.getDollarExchangeRate( ) );
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/oop/abst/Bank.java:
--------------------------------------------------------------------------------
1 | package oop.abst;
2 |
3 | public abstract class Bank {
4 |
5 | public abstract String getBankName( );
6 |
7 | protected abstract int getRate( );
8 |
9 | public int serviceChargeRate = 4;
10 |
11 | protected double getDollarExchangeRate( ) {
12 | // Write your logic to fetch dollar exchange rate.
13 | // For example we use constantc
14 | return 101.5;
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/abst/Nabil.java:
--------------------------------------------------------------------------------
1 | package oop.abst;
2 |
3 | public class Nabil extends Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "Nabil";
8 | }
9 |
10 | @Override
11 | protected int getRate( ) {
12 | return 6;
13 | }
14 |
15 | // You can write other variable and method here.
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/abst/NicAsia.java:
--------------------------------------------------------------------------------
1 | package oop.abst;
2 |
3 | public class NicAsia extends Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "NIC Asia";
8 | }
9 |
10 | @Override
11 | protected int getRate( ) {
12 | return 10;
13 | }
14 |
15 | // You can write other variable and method here.
16 | }
17 |
--------------------------------------------------------------------------------
/src/oop/abst/StandChart.java:
--------------------------------------------------------------------------------
1 | package oop.abst;
2 |
3 | public class StandChart extends Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "Standard Chartered";
8 | }
9 |
10 | @Override
11 | protected int getRate( ) {
12 | return 4;
13 | }
14 |
15 | // You can write other variable and method here.
16 | }
17 |
--------------------------------------------------------------------------------
/src/oop/block/Bike.java:
--------------------------------------------------------------------------------
1 | package oop.block;
2 |
3 | public class Bike {
4 |
5 | private int speed;
6 |
7 | public Bike() {
8 | System.out.println("Constrctor: " + speed);
9 | }
10 |
11 | {
12 | speed = 100;
13 | System.out.println("Block: " + speed);
14 | }
15 |
16 | public static void main(String[] args) {
17 | Bike bike = new Bike();
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/oop/finalex/EmployeePanNo.java:
--------------------------------------------------------------------------------
1 | package oop.finalex;
2 |
3 | public class EmployeePanNo {
4 | int id;
5 | String name;
6 | final int PAN_NO;
7 |
8 | public EmployeePanNo() {
9 | PAN_NO = 87290;
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/oop/finalex/FinalParam.java:
--------------------------------------------------------------------------------
1 | package oop.finalex;
2 |
3 | public class FinalParam {
4 |
5 | private double area = 0.0;
6 |
7 | public void area(final double PI, double r) {
8 | area = PI * r * r;
9 |
10 | PI = 2.5;
11 |
12 | System.out.println(area);
13 | }
14 |
15 | public static void main(String[] args) {
16 | FinalParam obj = new FinalParam();
17 | obj.area(Math.PI, 2);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/oop/inheritence/Bus.java:
--------------------------------------------------------------------------------
1 | package oop.inheritence;
2 |
3 | public class Bus extends Vehicle {
4 | private int cc;
5 | private int gears;
6 |
7 | public Bus( String color, int speed, int size, int cc, int gears ) {
8 | super( color, speed, size );
9 | this.cc = cc;
10 | this.gears = gears;
11 | }
12 |
13 | public void attributesBus( ) {
14 | System.out.println( "Color : " + super.getColor( ) );
15 | System.out.println( "Speed : " + super.speed );
16 | System.out.println( "Size : " + super.size );
17 |
18 | System.out.println( "CC: " + this.cc );
19 | System.out.println( "Gears: " + this.gears );
20 | }
21 |
22 | public static void main( String[] args ) {
23 | Bus b = new Bus( "RED", 100, 500, 150, 5 );
24 | b.attributesBus( );
25 |
26 | b.setColor( "GREEEN" );
27 | b.attributesBus( );
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/oop/inheritence/Car.java:
--------------------------------------------------------------------------------
1 | package oop.inheritence;
2 |
3 | public class Car extends Vehicle {
4 | private int cc;
5 | private int gears;
6 |
7 | public void attributesCar( ) {
8 | System.out.println( "Color : " + getColor( ) );
9 | System.out.println( "Speed : " + speed ); // super.speed or speed
10 | System.out.println( "Size : " + size );
11 |
12 | System.out.println( "CC: " + this.cc ); // this.cc or cc
13 | System.out.println( "Gears: " + gears );
14 | }
15 |
16 | public static void main( String[] args ) {
17 | Car c1 = new Car( );
18 | c1.cc = 1000;
19 | c1.gears = 5;
20 | c1.setVehicleProp( "RED", 80, 500 );
21 |
22 | c1.attributesCar( );
23 |
24 | System.out.println( c1.getColor( ) );
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/oop/inheritence/Vehicle.java:
--------------------------------------------------------------------------------
1 | package oop.inheritence;
2 |
3 | public class Vehicle {
4 |
5 | private String color;
6 | public int speed;
7 | protected int size;
8 |
9 | public Vehicle( ) {
10 |
11 | }
12 |
13 | public Vehicle( String color, int speed, int size ) {
14 | this.color = color;
15 | this.speed = speed;
16 | this.size = size;
17 | }
18 |
19 | public String getColor( ) {
20 | return color;
21 | }
22 |
23 | public void setColor( String color ) {
24 | this.color = color;
25 | }
26 |
27 | public void setVehicleProp( String color, int speed, int size ) {
28 | this.color = color;
29 | this.speed = speed;
30 | this.size = size;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/oop/interf/Bank.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public interface Bank {
4 |
5 | /*
6 | * Compiler add abstract keyword in these methods.
7 | */
8 | public String getBankName( );
9 |
10 | public int getRate( );
11 |
12 | /*
13 | * Its OK but not good to define Constant in the interface.
14 | * Compiler change it to public static final int serviceChargeRate = 4.
15 | */
16 | public int serviceChargeRate = 4;
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/interf/InterfaceImplTest.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public class InterfaceImplTest {
4 | public static void main( String[] args ) {
5 |
6 | Bank n = new Nabil( ); // OR This is also right: Nabil n = new Nabil( );
7 | printBankInfo( n );
8 |
9 | NicAsia na = new NicAsia( );
10 | printBankInfo( na );
11 |
12 | Bank sc = new StandChart( );
13 | printBankInfo( sc );
14 |
15 | }
16 |
17 | public static void printBankInfo( Bank b ) {
18 | System.out.println( "Bank Name: " + b.getBankName( ) + "\tInterest Rate: " + b.getRate( ) + "\t Service Charge Rate: " + Bank.serviceChargeRate );
19 | }
20 |
21 | /*
22 | * public static void printBankInfo( String bankName, double rate, int serviceChargeRate ) {
23 | * System.out.println( "Bank Name: " + bankName +
24 | * "\tInterest Rate: " + rate + "\t Service Charge Rate: " + serviceChargeRate );
25 | * }
26 | */
27 | }
28 |
--------------------------------------------------------------------------------
/src/oop/interf/Nabil.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public class Nabil implements Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "Nabil";
8 | }
9 |
10 | @Override
11 | public int getRate( ) {
12 | return 6;
13 | }
14 |
15 | // You can write other variable and method here.
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/interf/NicAsia.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public class NicAsia implements Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "NIC Asia";
8 | }
9 |
10 | @Override
11 | public int getRate( ) {
12 | return 10;
13 | }
14 |
15 | // You can write other variable and method here.
16 | }
17 |
--------------------------------------------------------------------------------
/src/oop/interf/Printable.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public interface Printable {
4 | void print( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/oop/interf/PrintableImpl.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public class PrintableImpl implements Printable {
4 |
5 | @Override
6 | public void print( ) {
7 | System.out.println( "PrintableImpl is printing..." );
8 | }
9 |
10 | public static void main( String[] args ) {
11 | Printable obj = new PrintableImpl( );
12 | obj.print( );
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/oop/interf/StandChart.java:
--------------------------------------------------------------------------------
1 | package oop.interf;
2 |
3 | public class StandChart implements Bank {
4 |
5 | @Override
6 | public String getBankName( ) {
7 | return "Standard Chartered";
8 | }
9 |
10 | @Override
11 | public int getRate( ) {
12 | return 4;
13 | }
14 | public int serviceChargeRate = 10;
15 | // You can write other variable and method here.
16 | }
17 |
--------------------------------------------------------------------------------
/src/oop/interf/ext/InterfaceInheritenceTest.java:
--------------------------------------------------------------------------------
1 | package oop.interf.ext;
2 |
3 | public class InterfaceInheritenceTest implements Showable, Readable {
4 |
5 | public static void main( String[] args ) {
6 |
7 | InterfaceInheritenceTest obj = new InterfaceInheritenceTest( );
8 | obj.print( );
9 | obj.read( );
10 | obj.show( );
11 | System.out.println( obj.hashCode( ) );
12 | // ---------
13 | Showable sObj = new InterfaceInheritenceTest( );
14 | sObj.print( );
15 | // sObj.read( ); //ERROR
16 | sObj.show( );
17 | ( ( Readable ) sObj ).read( ); // OK
18 | // ----------
19 | Printable pObj = new InterfaceInheritenceTest( );
20 | pObj.print( );
21 | // pObj.read( );
22 | // pObj.show( );
23 | ( ( Readable ) pObj ).read( );
24 | ( ( Showable ) pObj ).show( );
25 | // ---------------
26 | Readable rObj = new InterfaceInheritenceTest( );
27 | System.out.println( Readable.noOfPeopRead );
28 | // rObj.print( );
29 | rObj.read( );
30 | // rObj.show( );
31 |
32 | // Casting to another interface.
33 | ( ( Printable ) rObj ).print( );
34 | ( ( Showable ) rObj ).show( );
35 |
36 | }
37 |
38 | @Override
39 | public void print( ) {
40 | System.out.println( "PRINT" );
41 | }
42 |
43 | @Override
44 | public void read( ) {
45 | System.out.println( "READ" );
46 | }
47 |
48 | @Override
49 | public void show( ) {
50 | System.out.println( "SHOW" );
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/oop/interf/ext/Printable.java:
--------------------------------------------------------------------------------
1 | package oop.interf.ext;
2 |
3 | public interface Printable {
4 | void print( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/oop/interf/ext/Readable.java:
--------------------------------------------------------------------------------
1 | package oop.interf.ext;
2 |
3 | public interface Readable {
4 | int noOfPeopRead = 50;
5 |
6 | void read( );
7 | }
8 |
--------------------------------------------------------------------------------
/src/oop/interf/ext/Showable.java:
--------------------------------------------------------------------------------
1 | package oop.interf.ext;
2 |
3 | public interface Showable extends Printable {
4 | void show( );
5 | }
6 |
--------------------------------------------------------------------------------
/src/oop/overloading/AmbigiousCalculation.java:
--------------------------------------------------------------------------------
1 | package oop.overloading;
2 |
3 | public class AmbigiousCalculation {
4 | void sum(int a, long b) {
5 | System.out.println("a method invoked");
6 | }
7 |
8 | void sum(long a, int b) {
9 | System.out.println("b method invoked");
10 | }
11 |
12 | public static void main(String args[]) {
13 | AmbigiousCalculation obj = new AmbigiousCalculation();
14 | obj.sum(20, 20);// now ambiguity
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/overloading/Calculation.java:
--------------------------------------------------------------------------------
1 | package oop.overloading;
2 |
3 | public class Calculation {
4 |
5 | void sum(int a, int b) {
6 | System.out.println(a + b);
7 | }
8 |
9 | void sum(int a, int b, int c) {
10 | System.out.println(a + b + c);
11 | }
12 |
13 | public static void main(String args[]) {
14 | Calculation obj = new Calculation();
15 | obj.sum(10, 10, 10);
16 | obj.sum(20, 20);
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/oop/overloading/Calculation2.java:
--------------------------------------------------------------------------------
1 | package oop.overloading;
2 |
3 | public class Calculation2 {
4 |
5 | int sum( int a, int b ) {
6 | return ( a + b );
7 | }
8 |
9 | int sum( double a, double b ) {
10 | int r = ( int ) ( a + b );
11 | return r;
12 | }
13 |
14 | int sum( String a, String b ) {
15 | int r = Integer.valueOf( a ) + Integer.valueOf( b );
16 | return r;
17 | }
18 |
19 | public static void main( String args[] ) {
20 | Calculation2 obj = new Calculation2( );
21 | int r = obj.sum( 10.5, 10.5 );
22 | System.out.println( r );
23 |
24 | System.out.println( obj.sum( 10.5, 10.5 ) );
25 |
26 | System.out.println( obj.sum( "55", "44" ) );
27 |
28 | System.out.println( obj.sum( 100, 100 ) );
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/oop/overloading/OverloadingTypePromotionCalc.java:
--------------------------------------------------------------------------------
1 | package oop.overloading;
2 |
3 | public class OverloadingTypePromotionCalc {
4 |
5 | void sum(int a, int b) {
6 | System.out.println("int arg method invoked");
7 | }
8 |
9 | void sum(long a, long b) {
10 | System.out.println("long arg method invoked");
11 | }
12 |
13 | public static void main(String args[]) {
14 | OverloadingTypePromotionCalc obj = new OverloadingTypePromotionCalc();
15 | obj.sum(20, 20);// now int arg sum() method gets invoked
16 | obj.sum(50, 20);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/oop/override/Bike.java:
--------------------------------------------------------------------------------
1 | package oop.override;
2 |
3 | public class Bike extends Vehicle {
4 |
5 | @Override
6 | public void run( ) {
7 | super.run( );
8 | System.out.println( "Bike is Running!" );
9 | }
10 |
11 | public static void main( String[] args ) {
12 | Bike bike = new Bike( );
13 | bike.run( );
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/oop/override/Vehicle.java:
--------------------------------------------------------------------------------
1 | package oop.override;
2 |
3 | public class Vehicle {
4 |
5 | public void run() {
6 | System.out.println("Vehicle is Running!");
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/override/bank/Bank.java:
--------------------------------------------------------------------------------
1 | package oop.override.bank;
2 |
3 | public class Bank {
4 |
5 | protected int getRate() {
6 | return 0;
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/override/bank/InterestRateTest.java:
--------------------------------------------------------------------------------
1 | package oop.override.bank;
2 |
3 | public class InterestRateTest {
4 | public static void main(String[] args) {
5 | StandChart standChart = new StandChart();
6 | System.out.println("StandChart Rate: "+ standChart.getRate());
7 |
8 | Nabil nabil = new Nabil();
9 | System.out.println("Nabil Rate: "+ nabil.getRate());
10 |
11 | NicAsia nicAsia = new NicAsia();
12 | System.out.println("NicAsia Rate: "+ nicAsia.getRate());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/oop/override/bank/Nabil.java:
--------------------------------------------------------------------------------
1 | package oop.override.bank;
2 |
3 | public class Nabil extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 6;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/override/bank/NicAsia.java:
--------------------------------------------------------------------------------
1 | package oop.override.bank;
2 |
3 | public class NicAsia extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 10;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/override/bank/StandChart.java:
--------------------------------------------------------------------------------
1 | package oop.override.bank;
2 |
3 | public class StandChart extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 4;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/Bike.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism;
2 |
3 | public class Bike {
4 |
5 | public void run() {
6 | System.out.println("Bike is running.");
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/Splender.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism;
2 |
3 | public class Splender extends Bike {
4 |
5 | @Override
6 | public void run() {
7 | System.out.println("Running Splender Bike.");
8 | }
9 |
10 | public static void main(String[] args) {
11 | Bike b = new Splender(); //upcasting
12 | b.run();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/binding/Boy.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.binding;
2 |
3 | public class Boy extends Human {
4 |
5 | @Override
6 | public void walk() {
7 | System.out.println("Boy walks");
8 | }
9 |
10 | public static void main(String args[]) {
11 |
12 | Human h = new Human();
13 | h.walk(); // Static binding
14 |
15 | Boy b = new Boy(); // Static binding
16 | b.walk();
17 |
18 | Human db = new Boy();// Dynamic binding
19 | db.walk();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/binding/Human.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.binding;
2 |
3 | public class Human {
4 |
5 | public void walk() {
6 | System.out.println("Human walks");
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/multilevel/Animal.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.multilevel;
2 |
3 | public class Animal {
4 |
5 | void eat() {
6 | System.out.println("Class Animal: eating");
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/multilevel/BabyDog.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.multilevel;
2 |
3 | public class BabyDog extends Dog {
4 |
5 | void eat() {
6 | System.out.println("Class BabyDog: drinking milk");
7 | }
8 |
9 | public static void main(String[] args) {
10 | Animal a1 = new Animal();
11 | Animal a2 = new Dog();
12 | Animal a3 = new BabyDog();
13 |
14 | a1.eat();
15 | a2.eat();
16 | a3.eat();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/multilevel/Dog.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.multilevel;
2 |
3 | public class Dog extends Animal{
4 | void eat() {
5 | System.out.println("Class Dog: eating meet");
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/runtime/Bank.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.runtime;
2 |
3 | public class Bank {
4 |
5 | protected int getRate() {
6 | return 0;
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/runtime/Nabil.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.runtime;
2 |
3 | public class Nabil extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 6;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/runtime/NicAsia.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.runtime;
2 |
3 | public class NicAsia extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 10;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/runtime/RuntimePolymTest.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.runtime;
2 |
3 | public class RuntimePolymTest {
4 | public static void main(String[] args) {
5 | Bank standChart = new StandChart();
6 | System.out.println("StandChart Rate: " + standChart.getRate());
7 |
8 | Bank nabil = new Nabil();
9 | System.out.println("Nabil Rate: " + nabil.getRate());
10 |
11 | Bank nicAsia = new NicAsia();
12 | System.out.println("NicAsia Rate: " + nicAsia.getRate());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/oop/polymorphism/runtime/StandChart.java:
--------------------------------------------------------------------------------
1 | package oop.polymorphism.runtime;
2 |
3 | public class StandChart extends Bank {
4 |
5 | @Override
6 | protected int getRate() {
7 | return 4;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/oop/superex/Bike.java:
--------------------------------------------------------------------------------
1 | package oop.superex;
2 |
3 | public class Bike extends Vehicle {
4 |
5 | int speed = 100; // Km/Hour
6 |
7 | public void displaySpeed() {
8 | System.out.println("Bike Speed: " + speed);
9 | System.out.println("Vehicle Speed: " + super.speed);
10 | }
11 |
12 | public static void main(String[] args) {
13 | Bike bike = new Bike();
14 | bike.displaySpeed();
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/oop/superex/Vehicle.java:
--------------------------------------------------------------------------------
1 | package oop.superex;
2 |
3 | public class Vehicle {
4 |
5 | int speed = 50; //Km/Hour
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/org/Course.java:
--------------------------------------------------------------------------------
1 | package org;
2 |
3 | public class Course {
4 |
5 | private String jCourse = "Java";
6 |
7 | private void privateCourse() {
8 | System.out.println("I am private " + jCourse);
9 | }
10 |
11 | public void publicCourse() {
12 | System.out.println("I am public " + jCourse);
13 | }
14 |
15 | public static void main(String[] args) {
16 | Course course = new Course();
17 |
18 | System.out.println( course.jCourse );//Private variable can be access with in same Class Object.
19 |
20 | course.privateCourse(); //Private method can be access with in same Class Object.
21 |
22 | course.publicCourse();
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/org/EmployeeChild.java:
--------------------------------------------------------------------------------
1 | package org;
2 |
3 | import org.accm.Employee;
4 |
5 | public class EmployeeChild extends Employee{
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/org/EmployeeTest.java:
--------------------------------------------------------------------------------
1 | package org;
2 |
3 | import org.accm.Employee;
4 |
5 | public class EmployeeTest {
6 | public static void main(String[] args) {
7 | Employee emp = new Employee();
8 | System.out.println(emp.id);
9 | System.out.println(emp.name);
10 | System.out.println(emp.address);
11 | System.out.println(emp.email);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/org/Factorial.java:
--------------------------------------------------------------------------------
1 | package org;
2 |
3 | import java.util.Scanner;
4 |
5 | // 5! = 5 * 4 * 3 * 2 * 1
6 | public class Factorial {
7 | public static void main(String[] args) {
8 | Scanner sc = new Scanner(System.in);
9 |
10 | System.out.println("Enter Factorial No:");
11 | int no = sc.nextInt();
12 |
13 | long factNo = 1;
14 | for (int i = no; i >= 1; i--) {
15 | factNo = factNo * i;
16 | }
17 |
18 | System.out.println("Factorial of "+ no + " is: "+ factNo);
19 |
20 | sc.close();
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/org/accm/Employee.java:
--------------------------------------------------------------------------------
1 | package org.accm;
2 |
3 | public class Employee {
4 | private int id;
5 | private String name;
6 | private String email;
7 |
8 | //Setter
9 | public void setId(int id){
10 | this.id = id;
11 | }
12 | public void setName(String name){
13 | this.name = name;
14 | }
15 | public void setEmail(String email){
16 | this.email = email;
17 | }
18 |
19 | //GETTER
20 | public int getId(){
21 | return id;
22 | }
23 | public String getName(){
24 | return name;
25 | }
26 | public String getEmail(){
27 | return email;
28 | }
29 |
30 |
31 | @Override
32 | public String toString() {
33 | return id + " " + name + " " + email;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/org/accm/EmployeeMain.java:
--------------------------------------------------------------------------------
1 | package org.accm;
2 |
3 | public class EmployeeMain {
4 | public static void main(String[] args) {
5 | Employee emp = new Employee();
6 |
7 | //emp.id = 10;
8 | emp.setId(10);
9 |
10 | //emp.name = "SHYAM";
11 | emp.setName("SHYAM");
12 |
13 | //emp.email = "shyam@gmail.com";
14 | emp.setEmail("shyam@gmail.com");
15 |
16 | //GETTER
17 | System.out.println("ID: "+ emp.getId());
18 | System.out.println("Name: "+ emp.getName());
19 | System.out.println("Email: " + emp.getEmail());
20 |
21 |
22 | System.out.println(emp);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/org/accm/Student.java:
--------------------------------------------------------------------------------
1 | package org.accm;
2 |
3 | public class Student {
4 |
5 | private int id;
6 | private String name;
7 | private String email;
8 | private double fee;
9 | private String address;
10 |
11 | public int getId() {
12 | return id;
13 | }
14 |
15 | public void setId(int id) {
16 | this.id = id;
17 | }
18 | public String getName() {
19 | return name;
20 | }
21 |
22 | public void setName(String name) {
23 | this.name = name;
24 | }
25 |
26 |
27 |
28 | public String getEmail() {
29 | return email;
30 | }
31 |
32 | public void setEmail(String email) {
33 | this.email = email;
34 | }
35 |
36 |
37 |
38 | public double getFee() {
39 | return fee;
40 | }
41 |
42 |
43 |
44 | public void setFee(double fee) {
45 | this.fee = fee;
46 | }
47 |
48 |
49 |
50 | public String getAddress() {
51 | return address;
52 | }
53 |
54 |
55 |
56 | public void setAddress(String address) {
57 | this.address = address;
58 | }
59 |
60 |
61 |
62 | @Override
63 | public String toString() {
64 | return getId() + " " + name + " " + email + " " + fee + " " + address;
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/org/accm/User.java:
--------------------------------------------------------------------------------
1 | package org.accm;
2 |
3 | public class User {
4 | private int id;
5 | private String name;
6 | private String email;
7 |
8 | public User(int id, String name, String email) {
9 | this.id = id;
10 | this.name = name;
11 | this.email = email;
12 | }
13 |
14 | @Override
15 | public String toString() {
16 | return id + " "+ name + " " + email;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/org/accm/UserMain.java:
--------------------------------------------------------------------------------
1 | package org.accm;
2 |
3 | public class UserMain {
4 | public static void main(String[] args) {
5 | User usr = new User(5, "RAM", "abc@gmail.com");
6 | System.out.println(usr); // .toString()
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/org/constructor/Volume.java:
--------------------------------------------------------------------------------
1 | package org.constructor;
2 |
3 | public class Volume {
4 | int length;
5 | int breadth;
6 | int height;
7 |
8 | // Default Constructor
9 | public Volume() {
10 | }
11 |
12 | // Constructor overloading
13 | // Parameterized Constructor
14 | Volume(int length, int breadth, int height) {
15 | this.length = length;
16 | this.breadth = breadth;
17 | this.height = height;
18 | }
19 |
20 | // Method
21 | void showVolume() {
22 | int vol = this.length * this.breadth * this.height;
23 | System.out.println("Volume: " + vol);
24 | }
25 |
26 | void showArea() {
27 | int area = length * breadth;
28 | System.out.println("Area: " + area);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/org/constructor/VolumeTest.java:
--------------------------------------------------------------------------------
1 | package org.constructor;
2 |
3 | public class VolumeTest {
4 | public static void main(String[] args) {
5 | Volume v = new Volume();
6 | v.showVolume();
7 |
8 | Volume v2 = new Volume(5, 10, 15);
9 | v2.showVolume();
10 | v2.showArea();
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/org/course/AndroidCourse.java:
--------------------------------------------------------------------------------
1 | package org.course;
2 |
3 | public class AndroidCourse {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/org/course/JavaCourse.java:
--------------------------------------------------------------------------------
1 | package org.course;
2 |
3 | public class JavaCourse {
4 |
5 | public void showCourse() {
6 |
7 | System.out.println("Course: Java!.\n Package Name: org.course");
8 |
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/org/course/StaticImport.java:
--------------------------------------------------------------------------------
1 | package org.course;
2 |
3 | import static java.lang.System.out;
4 |
5 | public class StaticImport {
6 |
7 | public static void main( String[] args ) {
8 | out.println( "Static Import: Awesome Java Feature!" );
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/org/imports/service/BigDecimal.java:
--------------------------------------------------------------------------------
1 | package org.imports.service;
2 |
3 | public class BigDecimal {
4 | public static final double MAX_DIGIT = 3222.14544232;
5 | }
6 |
--------------------------------------------------------------------------------
/src/org/imports/service/UserService.java:
--------------------------------------------------------------------------------
1 | package org.imports.service;
2 |
3 | public class UserService {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/src/org/imports/util/FileUtil.java:
--------------------------------------------------------------------------------
1 | package org.imports.util;
2 |
3 | import java.io.File;
4 |
5 | /*import java.util.ArrayList;
6 | import java.util.Date;
7 | import java.util.Scanner;*/
8 |
9 | import java.util.*;
10 |
11 | import org.imports.service.BigDecimal;
12 | import org.imports.service.UserService;
13 |
14 | public class FileUtil {
15 | public static void main(String[] args) {
16 |
17 | // Date and Time: use java class Date
18 | Date d = new Date();
19 | System.out.println(d);
20 |
21 | ArrayList lst = new ArrayList();
22 |
23 | // ----------Read program---------
24 | Scanner sc = new Scanner(System.in);
25 |
26 | System.out.println("Enter your Name: ");
27 | String s = sc.nextLine();
28 | System.out.println("Name: " + s);
29 |
30 | System.out.println("Enter your age: ");
31 | int age = sc.nextInt();
32 | System.out.println("Age: " + age);
33 | // --------------------------
34 |
35 | UserService us = new UserService();
36 | File f = new File("D:\\abc.txt");
37 |
38 | // No import
39 | // 1. for same package classess
40 | MathUtils mathUtil = new MathUtils();
41 |
42 | // 2. For package: java.lang
43 | // Example String, Math
44 | System.out.println(Math.PI);
45 |
46 | BigDecimal bd = new BigDecimal();
47 | java.math.BigDecimal ten = new java.math.BigDecimal("10");
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/org/imports/util/MathUtils.java:
--------------------------------------------------------------------------------
1 | package org.imports.util;
2 |
3 | import static java.lang.System.out;
4 | import static org.imports.service.BigDecimal.MAX_DIGIT;
5 |
6 | public class MathUtils {
7 | public static void main(String[] args) {
8 | out.println(Math.PI);
9 | System.out.println(MAX_DIGIT); // BigDecimal.MAX_DIGIT
10 | System.out.println(MAX_DIGIT);
11 | System.out.println(MAX_DIGIT);
12 | System.out.println(MAX_DIGIT);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/org/multiclassex/InnerClassEx.java:
--------------------------------------------------------------------------------
1 | package org.multiclassex;
2 |
3 | public class InnerClassEx {
4 |
5 | private String val = "abc";
6 |
7 | public static void main(String[] args) {
8 |
9 | InnerClassEx inClassObj = new InnerClassEx();
10 | System.out.println(inClassObj.val);
11 |
12 | // Access Inner class only with obj.new ClassName
13 | Course course = inClassObj.new Course();
14 | System.out.println(course.course); // Accessing private variable:
15 | course.showCourse(); // Accessing private variable:
16 |
17 | org.Course crse = new org.Course();
18 | crse.publicCourse();
19 |
20 | }
21 |
22 | private class Course {
23 |
24 | private String course = "Java";
25 |
26 | public void showCourse() {
27 | System.out.println("Course is: " + course);
28 | }
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/org/multiclassex/MultipleClassEx.java:
--------------------------------------------------------------------------------
1 | package org.multiclassex;
2 |
3 | public class MultipleClassEx {
4 |
5 | public static void main(String[] args) {
6 | JavaCourse course = new JavaCourse();
7 | course.showCourse();
8 | }
9 |
10 | }
11 |
12 | class JavaCourse {
13 | private String jCourse = "Java";
14 |
15 | public void showCourse() {
16 | System.out.println("Course: " + jCourse);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/org/pkgpvt/JavaCourse.java:
--------------------------------------------------------------------------------
1 | package org.pkgpvt;
2 |
3 | public class JavaCourse {
4 |
5 | //package-private variable(no modifier)
6 | String jCourse = "Java";
7 |
8 | //package-private method(no modifier)
9 | void showCourse() {
10 | System.out.println("Course Name is: " + jCourse);
11 | }
12 |
13 | private String jAdvCourse = "Advance Java";
14 |
15 | public void showAdvJavaCourse(){
16 | System.out.println("Course Name is: " + jAdvCourse);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/org/pkgpvt/Student.java:
--------------------------------------------------------------------------------
1 | package org.pkgpvt;
2 |
3 | public class Student {
4 |
5 | public static void main(String[] args) {
6 |
7 | JavaCourse jc = new JavaCourse();
8 |
9 | //Package-Private
10 | System.out.println( jc.jCourse ); //Java
11 | jc.showCourse(); //Course Name is: Java
12 |
13 | //@UNCOMMENT jc.jAdvCourse; //Error: private field not visible
14 |
15 | jc.showAdvJavaCourse();//Course Name is: Advance Java
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/org/stud/Student.java:
--------------------------------------------------------------------------------
1 | package org.stud;
2 |
3 | import org.pkgpvt.JavaCourse;
4 |
5 | public class Student {
6 |
7 | public static void main(String[] args) {
8 |
9 | JavaCourse jc = new JavaCourse();
10 |
11 | //Package-Private
12 | //@UNCOMMENT System.out.println( jc.jCourse ); //Java
13 | //@UNCOMMENT jc.showCourse(); //Course Name is: Java
14 |
15 | //@UNCOMMENT jc.jAdvCourse; //Error: private field not visible
16 |
17 | jc.showAdvJavaCourse();//Course Name is: Advance Java
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/org/typeconversion/HelloWorld.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yrojha4ever/JavaStud/8b38dd73eee9eddf0e6e8cb4ee03ea916013eef3/src/org/typeconversion/HelloWorld.class
--------------------------------------------------------------------------------
/src/org/typeconversion/VarInitClass.java:
--------------------------------------------------------------------------------
1 | package org.typeconversion;
2 |
3 | public class VarInitClass {
4 |
5 | private int a = 10;
6 |
7 | public static void main(String[] args) {
8 | VarInitClass obj = new VarInitClass();
9 | System.out.println(obj.a);
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/pojo/StudPojoMain.java:
--------------------------------------------------------------------------------
1 | package pojo;
2 |
3 | public class StudPojoMain {
4 |
5 | public static void main(String[] args) {
6 | StudentPojo stud = new StudentPojo();
7 | stud.setId(10);
8 | stud.setName("RAM");
9 |
10 | int id = stud.getId();
11 | System.out.println("Id = : "+ id);
12 |
13 | System.out.println("Name: "+ stud.getName());
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/pojo/StudentPojo.java:
--------------------------------------------------------------------------------
1 | package pojo;
2 |
3 | public class StudentPojo {
4 | public StudentPojo(){
5 | }
6 | private int id;
7 | private String name;
8 |
9 |
10 | public int getId() {
11 | return id;
12 | }
13 | public void setId(int id) {
14 | this.id = id;
15 | }
16 |
17 |
18 | public String getName() {
19 | return name;
20 | }
21 | public void setName(String name) {
22 | this.name = name;
23 | }
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/str/Format.java:
--------------------------------------------------------------------------------
1 | package str;
2 |
3 | public class Format {
4 | public static void main( String[] args ) {
5 |
6 | String name = "Peter";
7 | String message = String.format( "Welcome %s, to Java Programming.", name );
8 | System.out.println( message );
9 |
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/str/StringBuffMain.java:
--------------------------------------------------------------------------------
1 | package str;
2 |
3 | public class StringBuffMain {
4 |
5 | public static void main(String[] args) {
6 | StringBuffer buffer = new StringBuffer();
7 | buffer.append("HELLO ");
8 | buffer.append("JAVA");
9 |
10 | String str = buffer.toString();
11 | System.out.println(buffer); //buffer.toString()
12 |
13 | StringBuilder builder = new StringBuilder();
14 | builder.append("HELLO ");
15 | builder.append("JAVA");
16 | System.out.println(builder);
17 |
18 | Integer inte = new Integer("25");
19 | Integer integ = Integer.parseInt("25");
20 | Integer integr = Integer.valueOf("25");
21 |
22 | int a = inte;
23 | System.out.println(a);
24 | inte = a;
25 | System.out.println(inte);
26 |
27 | System.out.println( Integer.toHexString(inte) );
28 |
29 |
30 | Double d = 10.8;
31 | int ab = d.intValue(); //(int)d;
32 | Float f = d.floatValue();
33 | byte b = (byte)ab;
34 |
35 |
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/str/StringBufferTest.java:
--------------------------------------------------------------------------------
1 | package str;
2 |
3 | public class StringBufferTest {
4 |
5 | public static void main(String[] args) {
6 | StringBuilder sb = new StringBuilder("Hello");
7 | sb.append("Java");
8 | System.out.println(sb);
9 |
10 | StringBuffer sf = new StringBuffer("Hello");
11 | sf.append("Java");
12 | System.out.println(sf);
13 |
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/str/StringEqual.java:
--------------------------------------------------------------------------------
1 | package str;
2 |
3 | public class StringEqual {
4 | public static void main(String[] args) {
5 | String hel = "Hello".substring(0, 3); // Hel
6 | System.out.println("String hel = " + hel);
7 |
8 | boolean eqVal = (hel == "Hel");
9 | System.out.println("hel == 'Hel' is: " + eqVal);
10 |
11 | boolean eqFun = hel.equals("Hel");
12 | System.out.println("hel.equals('Hel') is: " + eqFun);
13 |
14 | String h = hel;
15 | System.out.println(h == hel);
16 |
17 | hel = "abc";
18 | System.out.println(h);
19 | Integer a = new Integer(2);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/str/WrapperIntro.java:
--------------------------------------------------------------------------------
1 | package str;
2 |
3 | public class WrapperIntro {
4 |
5 | public static void main(String[] args) {
6 | Integer intObj1 = new Integer (25);
7 |
8 | Integer intObj2 = new Integer ("25");
9 |
10 | Integer intObj3= new Integer (35);
11 |
12 | //compareTo demo
13 | System.out.println("Comparing using compareTo Obj1 and Obj2: " + intObj1.compareTo(intObj2));
14 |
15 | System.out.println("Comparing using compareTo Obj1 and Obj3: " + intObj1.compareTo(intObj3));
16 |
17 | //Equals demo
18 | System.out.println("Comparing using equals Obj1 and Obj2: " + intObj1.equals(intObj2));
19 |
20 | System.out.println("Comparing using equals Obj1 and Obj3: " + intObj1.equals(intObj3));
21 |
22 |
23 | Float f1 = new Float("2.25f");
24 |
25 | Float f2 = new Float("20.43f");
26 |
27 | Float f3 = new Float(2.25f);
28 |
29 | System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2));
30 |
31 | System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3));
32 |
33 |
34 | //Addition of Integer with Float
35 | Float f = intObj1.floatValue() + f1;
36 |
37 | System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+"=" +f );
38 |
39 | String str = "5";
40 | int i = Integer.parseInt(str);
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/swing/EventHandlingAdapter.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.Color;
4 | import java.awt.EventQueue;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.ActionListener;
7 | import java.awt.event.FocusAdapter;
8 | import java.awt.event.FocusEvent;
9 |
10 | import javax.swing.JButton;
11 | import javax.swing.JFrame;
12 | import javax.swing.JLabel;
13 | import javax.swing.JOptionPane;
14 | import javax.swing.JPanel;
15 | import javax.swing.JTextField;
16 |
17 | public class EventHandlingAdapter extends JFrame {
18 | private static final long serialVersionUID = 6381888569965913982L;
19 |
20 | private JPanel contentPane;
21 | private JLabel nameLbl;
22 | private JTextField nameTxt;
23 | private JButton okBtn;
24 | private JLabel statusTxt;
25 |
26 | /**
27 | * Launch the application.
28 | */
29 | public static void main( String[] args ) {
30 | EventQueue.invokeLater( new Runnable( ) { // EventQueue or SwingUtilities
31 | public void run( ) {
32 | try {
33 | EventHandlingAdapter frame = new EventHandlingAdapter( );
34 | frame.setVisible( true );
35 | } catch ( Exception e ) {
36 | e.printStackTrace( );
37 | }
38 | }
39 | } );
40 | }
41 |
42 | /**
43 | * Create the frame.
44 | */
45 | public EventHandlingAdapter( ) {
46 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
47 | setBounds( 100, 100, 450, 300 );
48 | contentPane = new JPanel( );
49 | setContentPane( contentPane );
50 | contentPane.setLayout( null );
51 | contentPane.add( getNameLbl( ) );
52 | contentPane.add( getNameTxt( ) );
53 | contentPane.add( getOkBtn( ) );
54 | contentPane.add( getStatusTxt( ) );
55 | }
56 |
57 | private JLabel getNameLbl( ) {
58 | if ( nameLbl == null ) {
59 | nameLbl = new JLabel( "Write Your Name:" );
60 | nameLbl.setForeground( Color.BLUE );
61 | nameLbl.setBounds( 32, 81, 105, 25 );
62 | }
63 | return nameLbl;
64 | }
65 |
66 | private JTextField getNameTxt( ) {
67 | if ( nameTxt == null ) {
68 | nameTxt = new JTextField( );
69 | nameTxt.setBounds( 136, 83, 129, 25 );
70 | nameTxt.addFocusListener( new FocusAdapter( ) {
71 |
72 | @Override
73 | public void focusLost( FocusEvent e ) {
74 | statusTxt.setText( "Focus Losted: " + nameTxt.getText( ) );
75 | }
76 |
77 | } );
78 | }
79 | return nameTxt;
80 | }
81 |
82 | private JButton getOkBtn( ) {
83 | if ( okBtn == null ) {
84 | okBtn = new JButton( "OK" );
85 | okBtn.setBounds( 300, 82, 89, 23 );
86 | okBtn.addActionListener( new ActionListener( ) {
87 | public void actionPerformed( ActionEvent e ) {
88 | JOptionPane.showMessageDialog( contentPane, "Your name is: " + nameTxt.getText( ) );
89 | }
90 | } );
91 | }
92 | return okBtn;
93 | }
94 |
95 | private JLabel getStatusTxt( ) {
96 | if ( statusTxt == null ) {
97 | statusTxt = new JLabel( );
98 | statusTxt.setEnabled( false );
99 | statusTxt.setBounds( 34, 231, 355, 20 );
100 | }
101 | return statusTxt;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/swing/EventHandlingListener.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.Color;
4 | import java.awt.EventQueue;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.ActionListener;
7 | import java.awt.event.FocusEvent;
8 | import java.awt.event.FocusListener;
9 |
10 | import javax.swing.JButton;
11 | import javax.swing.JFrame;
12 | import javax.swing.JLabel;
13 | import javax.swing.JOptionPane;
14 | import javax.swing.JPanel;
15 | import javax.swing.JTextField;
16 |
17 | public class EventHandlingListener extends JFrame {
18 | private static final long serialVersionUID = 6381888569965913982L;
19 |
20 | private JPanel contentPane;
21 | private JLabel nameLbl;
22 | private JTextField nameTxt;
23 | private JButton okBtn;
24 | private JLabel statusTxt;
25 |
26 | /**
27 | * Launch the application.
28 | */
29 | public static void main( String[] args ) {
30 | EventQueue.invokeLater( new Runnable( ) { // EventQueue or SwingUtilities
31 | public void run( ) {
32 | try {
33 | EventHandlingListener frame = new EventHandlingListener( );
34 | frame.setVisible( true );
35 | } catch ( Exception e ) {
36 | e.printStackTrace( );
37 | }
38 | }
39 | } );
40 | }
41 |
42 | /**
43 | * Create the frame.
44 | */
45 | public EventHandlingListener( ) {
46 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
47 | setBounds( 100, 100, 450, 300 );
48 | contentPane = new JPanel( );
49 | setContentPane( contentPane );
50 | contentPane.setLayout( null );
51 | contentPane.add( getNameLbl( ) );
52 | contentPane.add( getNameTxt( ) );
53 | contentPane.add( getOkBtn( ) );
54 | contentPane.add( getStatusTxt( ) );
55 | }
56 |
57 | private JLabel getNameLbl( ) {
58 | if ( nameLbl == null ) {
59 | nameLbl = new JLabel( "Write Your Name:" );
60 | nameLbl.setForeground( Color.BLUE );
61 | nameLbl.setBounds( 32, 81, 105, 25 );
62 | }
63 | return nameLbl;
64 | }
65 |
66 | private JTextField getNameTxt( ) {
67 | if ( nameTxt == null ) {
68 | nameTxt = new JTextField( );
69 | nameTxt.setBounds( 136, 83, 129, 25 );
70 | nameTxt.addFocusListener( new FocusListener( ) {
71 |
72 | @Override
73 | public void focusLost( FocusEvent e ) {
74 | statusTxt.setText( "Focus Losted: " + nameTxt.getText( ) );
75 | }
76 |
77 | @Override
78 | public void focusGained( FocusEvent e ) {
79 | statusTxt.setText( "Focus Gained: " + nameTxt.getText( ) );
80 | }
81 | } );
82 | }
83 | return nameTxt;
84 | }
85 |
86 | private JButton getOkBtn( ) {
87 | if ( okBtn == null ) {
88 | okBtn = new JButton( "OK" );
89 | okBtn.setBounds( 300, 82, 89, 23 );
90 | okBtn.addActionListener( new ActionListener( ) {
91 | public void actionPerformed( ActionEvent e ) {
92 | JOptionPane.showMessageDialog( contentPane, "Your name is: " + nameTxt.getText( ) );
93 | }
94 | } );
95 | }
96 | return okBtn;
97 | }
98 |
99 | private JLabel getStatusTxt( ) {
100 | if ( statusTxt == null ) {
101 | statusTxt = new JLabel( );
102 | statusTxt.setEnabled( false );
103 | statusTxt.setBounds( 34, 231, 355, 20 );
104 | }
105 | return statusTxt;
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/swing/ExtendtedJFrame.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import javax.swing.JButton;
4 | import javax.swing.JFrame;
5 | import javax.swing.JLabel;
6 | import javax.swing.JPanel;
7 |
8 | public class ExtendtedJFrame extends JFrame {
9 | private static final long serialVersionUID = 563395041350405111L;
10 |
11 | private JButton button = null;
12 | private JLabel label = null;
13 |
14 | public ExtendtedJFrame( ) {
15 | initializeComp( );
16 | this.setTitle( "JFrame Extended: JavaStud" );
17 | this.setSize( 400, 450 );
18 | this.setLayout( null );// using no layout managers
19 | }
20 |
21 | public void initializeComp( ) {
22 | JPanel contentPane = new JPanel( );
23 | contentPane.setLayout( null );
24 | contentPane.setSize( 400, 450 );
25 |
26 | contentPane.add( getLabel( ) ); //label
27 | contentPane.add( getButton( ) ); //button
28 |
29 | this.getContentPane( ).add( contentPane );
30 | }
31 |
32 | private JButton getButton( ) {
33 | if ( button == null ) {
34 | button = new JButton( "Click" );
35 | button.setBounds( 165, 100, 100, 40 );
36 | }
37 | return button;
38 | }
39 |
40 | private JLabel getLabel( ) {
41 | if ( label == null ) {
42 | label = new JLabel( "First Java Swing Program!" );
43 | label.setLocation( 10, 100 );
44 | label.setSize( 150, 25 );
45 | }
46 | return label;
47 | }
48 |
49 | public static void main( String[] args ) {
50 |
51 | ExtendtedJFrame mainFrame = new ExtendtedJFrame( );
52 |
53 | mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Enable Close Button in window
54 |
55 | mainFrame.setVisible( true );// making the frame visible
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/swing/ImageButton.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import javax.swing.ImageIcon;
4 | import javax.swing.JButton;
5 | import javax.swing.JFrame;
6 |
7 | public class ImageButton {
8 | public ImageButton( ) {
9 | JFrame f = new JFrame( );
10 |
11 | JButton b = new JButton( new ImageIcon( "resource/pressit.jpg" ) );
12 | b.setBounds( 130, 100, 200, 80 );
13 |
14 | f.add( b );
15 |
16 | f.setSize( 450, 400 );
17 | f.setLayout( null );
18 | f.setVisible( true );
19 |
20 | f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
21 |
22 | }
23 |
24 | public static void main( String[] args ) {
25 | new ImageButton( );
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/swing/LoginScreenLayout.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.Color;
5 | import java.awt.EventQueue;
6 | import java.awt.FlowLayout;
7 | import java.awt.Font;
8 | import java.awt.event.ActionEvent;
9 | import java.awt.event.ActionListener;
10 |
11 | import javax.swing.JButton;
12 | import javax.swing.JFrame;
13 | import javax.swing.JLabel;
14 | import javax.swing.JPanel;
15 | import javax.swing.JTextField;
16 | import javax.swing.UIManager;
17 | import javax.swing.border.EmptyBorder;
18 | import javax.swing.border.EtchedBorder;
19 | import javax.swing.border.TitledBorder;
20 |
21 | import com.jgoodies.forms.factories.FormFactory;
22 | import com.jgoodies.forms.layout.ColumnSpec;
23 | import com.jgoodies.forms.layout.FormLayout;
24 | import com.jgoodies.forms.layout.RowSpec;
25 | import java.awt.GridLayout;
26 | import javax.swing.JPasswordField;
27 |
28 | public class LoginScreenLayout extends JFrame {
29 |
30 | private JPanel contentPane;
31 | private JPanel panelTop;
32 | private JPanel panelBottom;
33 | private JPanel panelLeft;
34 | private JPanel panelRight;
35 | private JPanel panelCenter;
36 | private JLabel lblUserName;
37 | private JTextField username;
38 | private JLabel lblPassword;
39 | private JPasswordField password;
40 | private JButton btnSignIn;
41 | private JButton btnCancel;
42 | private JPanel loginPanel;
43 | private JLabel status;
44 |
45 | /**
46 | * Launch the application.
47 | */
48 | public static void main( String[] args ) {
49 | EventQueue.invokeLater( new Runnable( ) {
50 | public void run( ) {
51 | try {
52 | LoginScreenLayout frame = new LoginScreenLayout( );
53 | frame.setVisible( true );
54 | } catch ( Exception e ) {
55 | e.printStackTrace( );
56 | }
57 | }
58 | } );
59 | }
60 |
61 | /**
62 | * Create the frame.
63 | */
64 | public LoginScreenLayout( ) {
65 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
66 | setBounds( 100, 100, 499, 261 );
67 | contentPane = new JPanel( );
68 | contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
69 | setContentPane( contentPane );
70 | contentPane.setLayout( new BorderLayout( -2, -2 ) );
71 | contentPane.add( getPanel_1( ), BorderLayout.NORTH );
72 | contentPane.add( getPanel_1_1( ), BorderLayout.SOUTH );
73 | contentPane.add( getPanelLeft( ), BorderLayout.WEST );
74 | contentPane.add( getPanelRight( ), BorderLayout.EAST );
75 | contentPane.add( getPanelCenter( ), BorderLayout.CENTER );
76 | }
77 |
78 | private JPanel getPanel_1( ) {
79 | if ( panelTop == null ) {
80 | panelTop = new JPanel( );
81 | FlowLayout flowLayout = ( FlowLayout ) panelTop.getLayout( );
82 | flowLayout.setVgap( 10 );
83 | flowLayout.setHgap( 10 );
84 | panelTop.setBackground( UIManager.getColor( "activeCaption" ) );
85 | panelTop.setForeground( Color.BLACK );
86 | panelTop.setBorder( null );
87 | }
88 | return panelTop;
89 | }
90 |
91 | private JPanel getPanel_1_1( ) {
92 | if ( panelBottom == null ) {
93 | panelBottom = new JPanel( );
94 | FlowLayout flowLayout = ( FlowLayout ) panelBottom.getLayout( );
95 | flowLayout.setVgap( 10 );
96 | flowLayout.setHgap( 10 );
97 | panelBottom.setBackground( UIManager.getColor( "activeCaption" ) );
98 | panelBottom.setForeground( Color.BLACK );
99 | }
100 | return panelBottom;
101 | }
102 |
103 | private JPanel getPanelLeft( ) {
104 | if ( panelLeft == null ) {
105 | panelLeft = new JPanel( );
106 | FlowLayout flowLayout = ( FlowLayout ) panelLeft.getLayout( );
107 | flowLayout.setVgap( 10 );
108 | flowLayout.setHgap( 10 );
109 | panelLeft.setBackground( UIManager.getColor( "activeCaption" ) );
110 | panelLeft.setForeground( Color.BLACK );
111 | }
112 | return panelLeft;
113 | }
114 |
115 | private JPanel getPanelRight( ) {
116 | if ( panelRight == null ) {
117 | panelRight = new JPanel( );
118 | FlowLayout flowLayout = ( FlowLayout ) panelRight.getLayout( );
119 | flowLayout.setVgap( 10 );
120 | flowLayout.setHgap( 10 );
121 | panelRight.setBackground( UIManager.getColor( "activeCaption" ) );
122 | panelRight.setForeground( Color.BLACK );
123 | }
124 | return panelRight;
125 | }
126 |
127 | private JPanel getPanelCenter( ) {
128 | if ( panelCenter == null ) {
129 | panelCenter = new JPanel( );
130 | panelCenter.setBackground( new Color( 204, 255, 204 ) );
131 | panelCenter.setBorder( new EtchedBorder( EtchedBorder.LOWERED, null, null ) );
132 | panelCenter.setForeground( Color.BLACK );
133 | panelCenter.setLayout( new FormLayout(new ColumnSpec[] {
134 | FormFactory.RELATED_GAP_COLSPEC,
135 | ColumnSpec.decode("default:grow"),
136 | ColumnSpec.decode("6dlu"),
137 | FormFactory.DEFAULT_COLSPEC,
138 | FormFactory.RELATED_GAP_COLSPEC,
139 | FormFactory.DEFAULT_COLSPEC,
140 | FormFactory.RELATED_GAP_COLSPEC,
141 | FormFactory.DEFAULT_COLSPEC,
142 | FormFactory.RELATED_GAP_COLSPEC,
143 | ColumnSpec.decode("default:grow"),},
144 | new RowSpec[] {
145 | FormFactory.RELATED_GAP_ROWSPEC,
146 | RowSpec.decode("default:grow"),
147 | FormFactory.RELATED_GAP_ROWSPEC,
148 | FormFactory.DEFAULT_ROWSPEC,
149 | FormFactory.RELATED_GAP_ROWSPEC,
150 | FormFactory.DEFAULT_ROWSPEC,
151 | FormFactory.RELATED_GAP_ROWSPEC,
152 | FormFactory.DEFAULT_ROWSPEC,
153 | FormFactory.RELATED_GAP_ROWSPEC,
154 | FormFactory.DEFAULT_ROWSPEC,
155 | FormFactory.RELATED_GAP_ROWSPEC,
156 | FormFactory.DEFAULT_ROWSPEC,
157 | FormFactory.RELATED_GAP_ROWSPEC,
158 | FormFactory.DEFAULT_ROWSPEC,
159 | FormFactory.RELATED_GAP_ROWSPEC,
160 | FormFactory.DEFAULT_ROWSPEC,}) );
161 | panelCenter.add( getLoginPanel( ), "2, 2, 9, 8, fill, fill" );
162 | panelCenter.add(getStatus(), "8, 10, 3, 1");
163 | panelCenter.add( getBtnSignIn( ), "4, 14" );
164 | panelCenter.add( getBtnCancel( ), "10, 14" );
165 | }
166 | return panelCenter;
167 | }
168 |
169 | private JLabel getLblUserName( ) {
170 | if ( lblUserName == null ) {
171 | lblUserName = new JLabel( "User Name:" );
172 | lblUserName.setFont( new Font( "Tahoma", Font.BOLD, 13 ) );
173 | }
174 | return lblUserName;
175 | }
176 |
177 | private JTextField getUsername( ) {
178 | if ( username == null ) {
179 | username = new JTextField( );
180 | username.setSize( 250, 30 );
181 | username.setColumns( 10 );
182 | }
183 | return username;
184 | }
185 |
186 | private JLabel getLblPassword( ) {
187 | if ( lblPassword == null ) {
188 | lblPassword = new JLabel( "Password:" );
189 | lblPassword.setFont( new Font( "Tahoma", Font.BOLD, 13 ) );
190 | }
191 | return lblPassword;
192 | }
193 |
194 | private JPasswordField getPassword( ) {
195 | if ( password == null ) {
196 | password = new JPasswordField( );
197 | password.setColumns( 10 );
198 | }
199 | return password;
200 | }
201 |
202 | private JButton getBtnSignIn( ) {
203 | if ( btnSignIn == null ) {
204 | btnSignIn = new JButton( "Sign In" );
205 | btnSignIn.setFont( new Font( "Tahoma", Font.BOLD, 11 ) );
206 | }
207 | return btnSignIn;
208 | }
209 |
210 | private JButton getBtnCancel( ) {
211 | if ( btnCancel == null ) {
212 | btnCancel = new JButton( "Cancel" );
213 | btnCancel.addActionListener( new ActionListener( ) {
214 | @Override
215 | public void actionPerformed( ActionEvent e ) {
216 | System.exit( 0 );
217 | }
218 | } );
219 | }
220 | return btnCancel;
221 | }
222 |
223 | private JPanel getLoginPanel( ) {
224 | if ( loginPanel == null ) {
225 | loginPanel = new JPanel( );
226 | loginPanel.setBorder( new TitledBorder( UIManager.getBorder( "TitledBorder.border" ), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color( 0, 0, 0 ) ) );
227 | loginPanel.setLayout(new GridLayout(2, 3, 2, 2));
228 | loginPanel.add( getLblUserName( ) );
229 | loginPanel.add( getUsername( ) );
230 | loginPanel.add( getLblPassword( ) );
231 | loginPanel.add( getPassword( ) );
232 | }
233 | return loginPanel;
234 | }
235 |
236 | private JLabel getStatus() {
237 | if (status == null) {
238 | status = new JLabel("Status:");
239 | status.setForeground(new Color(204, 0, 102));
240 | status.setFont(new Font("Tahoma", Font.BOLD, 11));
241 | }
242 | return status;
243 | }
244 | }
245 |
--------------------------------------------------------------------------------
/src/swing/SwingComponets.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.Color;
4 | import java.awt.EventQueue;
5 |
6 | import javax.swing.JButton;
7 | import javax.swing.JFrame;
8 | import javax.swing.JLabel;
9 | import javax.swing.JPanel;
10 | import javax.swing.JRadioButton;
11 | import javax.swing.JScrollPane;
12 | import javax.swing.JSeparator;
13 | import javax.swing.JTable;
14 | import javax.swing.JTextField;
15 | import javax.swing.UIManager;
16 | import javax.swing.border.EmptyBorder;
17 | import javax.swing.border.TitledBorder;
18 | import javax.swing.table.DefaultTableModel;
19 |
20 | public class SwingComponets extends JFrame {
21 |
22 | private JPanel contentPane;
23 | private JPanel mainPanel;
24 | private JPanel panel;
25 | private JLabel nameLbl;
26 | private JTextField nameTxt;
27 | private JLabel lblRollNo;
28 | private JTextField textField;
29 | private JLabel lblSubject;
30 | private JTextField textField_1;
31 | private JLabel lblFaculty;
32 | private JTextField textField_2;
33 | private JLabel lblBirthDate;
34 | private JTextField textField_3;
35 | private JLabel lblCollegeName;
36 | private JTextField textField_4;
37 | private JRadioButton rdbtnNewRadioButton;
38 | private JRadioButton rdbtnNewRadioButton_1;
39 | private JPanel sexPanel;
40 | private JButton btnSave;
41 | private JTable studTable;
42 | private JScrollPane scrollPane;
43 | private JSeparator separator;
44 |
45 | /**
46 | * Launch the application.
47 | */
48 | public static void main(String[] args) {
49 | EventQueue.invokeLater(new Runnable() {
50 | public void run() {
51 | try {
52 | SwingComponets frame = new SwingComponets();
53 | frame.setVisible(true);
54 | } catch (Exception e) {
55 | e.printStackTrace();
56 | }
57 | }
58 | });
59 | }
60 |
61 | /**
62 | * Create the frame.
63 | */
64 | public SwingComponets() {
65 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 | setBounds(100, 100, 715, 461);
67 | contentPane = new JPanel();
68 | contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
69 | setContentPane(contentPane);
70 | contentPane.setLayout(null);
71 | contentPane.add(getMainPanel());
72 | }
73 |
74 | private JPanel getMainPanel() {
75 | if (mainPanel == null) {
76 | mainPanel = new JPanel();
77 | mainPanel.setBounds(0, 0, 698, 487);
78 | mainPanel.setLayout(null);
79 | mainPanel.add(getPanel());
80 | mainPanel.add(getScrollPane());
81 | mainPanel.add(getSeparator());
82 | }
83 | return mainPanel;
84 | }
85 |
86 | private JPanel getPanel() {
87 | if (panel == null) {
88 | panel = new JPanel();
89 | panel.setBorder(new TitledBorder(UIManager
90 | .getBorder("TitledBorder.border"), "Student Form",
91 | TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,
92 | 0, 0)));
93 | panel.setBounds(10, 11, 678, 128);
94 | panel.setLayout(null);
95 | panel.add(getNameLbl());
96 | panel.add(getNameTxt());
97 | panel.add(getLblRollNo());
98 | panel.add(getTextField());
99 | panel.add(getLblSubject());
100 | panel.add(getTextField_1());
101 | panel.add(getLblFaculty());
102 | panel.add(getTextField_2());
103 | panel.add(getLblBirthDate());
104 | panel.add(getTextField_3());
105 | panel.add(getLblCollegeName());
106 | panel.add(getTextField_4());
107 | panel.add(getSexPanel());
108 | panel.add(getBtnSave());
109 | }
110 | return panel;
111 | }
112 |
113 | private JLabel getNameLbl() {
114 | if (nameLbl == null) {
115 | nameLbl = new JLabel("Name");
116 | nameLbl.setBounds(21, 22, 48, 14);
117 | }
118 | return nameLbl;
119 | }
120 |
121 | private JTextField getNameTxt() {
122 | if (nameTxt == null) {
123 | nameTxt = new JTextField();
124 | nameTxt.setBounds(65, 22, 121, 20);
125 | nameTxt.setColumns(10);
126 | }
127 | return nameTxt;
128 | }
129 |
130 | private JLabel getLblRollNo() {
131 | if (lblRollNo == null) {
132 | lblRollNo = new JLabel("Roll No:");
133 | lblRollNo.setBounds(430, 22, 46, 14);
134 | }
135 | return lblRollNo;
136 | }
137 |
138 | private JTextField getTextField() {
139 | if (textField == null) {
140 | textField = new JTextField();
141 | textField.setBounds(486, 22, 121, 20);
142 | textField.setColumns(10);
143 | }
144 | return textField;
145 | }
146 |
147 | private JLabel getLblSubject() {
148 | if (lblSubject == null) {
149 | lblSubject = new JLabel("Semester");
150 | lblSubject.setBounds(208, 51, 48, 14);
151 | }
152 | return lblSubject;
153 | }
154 |
155 | private JTextField getTextField_1() {
156 | if (textField_1 == null) {
157 | textField_1 = new JTextField();
158 | textField_1.setColumns(10);
159 | textField_1.setBounds(65, 48, 121, 20);
160 | }
161 | return textField_1;
162 | }
163 |
164 | private JLabel getLblFaculty() {
165 | if (lblFaculty == null) {
166 | lblFaculty = new JLabel("Faculty");
167 | lblFaculty.setBounds(21, 47, 46, 14);
168 | }
169 | return lblFaculty;
170 | }
171 |
172 | private JTextField getTextField_2() {
173 | if (textField_2 == null) {
174 | textField_2 = new JTextField();
175 | textField_2.setColumns(10);
176 | textField_2.setBounds(486, 48, 121, 20);
177 | }
178 | return textField_2;
179 | }
180 |
181 | private JLabel getLblBirthDate() {
182 | if (lblBirthDate == null) {
183 | lblBirthDate = new JLabel("Birth Date");
184 | lblBirthDate.setBounds(208, 22, 48, 14);
185 | }
186 | return lblBirthDate;
187 | }
188 |
189 | private JTextField getTextField_3() {
190 | if (textField_3 == null) {
191 | textField_3 = new JTextField();
192 | textField_3.setColumns(10);
193 | textField_3.setBounds(266, 22, 121, 20);
194 | }
195 | return textField_3;
196 | }
197 |
198 | private JLabel getLblCollegeName() {
199 | if (lblCollegeName == null) {
200 | lblCollegeName = new JLabel("College Name");
201 | lblCollegeName.setBounds(397, 48, 74, 14);
202 | }
203 | return lblCollegeName;
204 | }
205 |
206 | private JTextField getTextField_4() {
207 | if (textField_4 == null) {
208 | textField_4 = new JTextField();
209 | textField_4.setColumns(10);
210 | textField_4.setBounds(266, 48, 121, 20);
211 | }
212 | return textField_4;
213 | }
214 |
215 | private JRadioButton getRdbtnNewRadioButton() {
216 | if (rdbtnNewRadioButton == null) {
217 | rdbtnNewRadioButton = new JRadioButton("Male");
218 | rdbtnNewRadioButton.setBounds(6, 18, 54, 23);
219 | }
220 | return rdbtnNewRadioButton;
221 | }
222 |
223 | private JRadioButton getRdbtnNewRadioButton_1() {
224 | if (rdbtnNewRadioButton_1 == null) {
225 | rdbtnNewRadioButton_1 = new JRadioButton("Female");
226 | rdbtnNewRadioButton_1.setBounds(62, 18, 64, 23);
227 | }
228 | return rdbtnNewRadioButton_1;
229 | }
230 |
231 | private JPanel getSexPanel() {
232 | if (sexPanel == null) {
233 | sexPanel = new JPanel();
234 | sexPanel.setBorder(new TitledBorder(null, "Sex",
235 | TitledBorder.LEADING, TitledBorder.TOP, null, null));
236 | sexPanel.setBounds(21, 72, 164, 48);
237 | sexPanel.setLayout(null);
238 | sexPanel.add(getRdbtnNewRadioButton());
239 | sexPanel.add(getRdbtnNewRadioButton_1());
240 | }
241 | return sexPanel;
242 | }
243 |
244 | private JButton getBtnSave() {
245 | if (btnSave == null) {
246 | btnSave = new JButton("Save");
247 | btnSave.setBounds(496, 79, 89, 23);
248 | }
249 | return btnSave;
250 | }
251 |
252 | private JTable getStudTable() {
253 | if (studTable == null) {
254 | studTable = new JTable();
255 | studTable.setModel(new DefaultTableModel(new Object[][] {
256 |
257 | }, new String[] { "ID", "NAME", "RollNo", "Faculty", "Sex",
258 | "College Name" }));
259 | }
260 | return studTable;
261 | }
262 |
263 | private JScrollPane getScrollPane() {
264 | if (scrollPane == null) {
265 | scrollPane = new JScrollPane();
266 | scrollPane.setBounds(10, 189, 678, 195);
267 | scrollPane.setViewportView(getStudTable());
268 | }
269 | return scrollPane;
270 | }
271 |
272 | private JSeparator getSeparator() {
273 | if (separator == null) {
274 | separator = new JSeparator();
275 | separator.setBounds(10, 164, 678, 3);
276 | }
277 | return separator;
278 | }
279 | }
280 |
--------------------------------------------------------------------------------
/src/swing/SwingContentPaneExample.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import javax.swing.JButton;
4 | import javax.swing.JFrame;
5 | import javax.swing.JLabel;
6 | import javax.swing.JPanel;
7 |
8 | public class SwingContentPaneExample {
9 | public static void main( String[] args ) {
10 |
11 | JFrame f = new JFrame( );// creating instance of JFrame
12 | f.setTitle( "First Swing Program: JavaStud" );
13 |
14 | JPanel contentPane = new JPanel( );
15 | contentPane.setLayout( null );
16 | contentPane.setSize( 400, 450 );
17 |
18 | JLabel label = new JLabel( "First Java Swing Program!" );
19 | label.setLocation( 10, 100 );
20 | label.setSize( 150, 25 );
21 |
22 | JButton button = new JButton( "Click" );// creating instance of JButton
23 | button.setBounds( 165, 100, 100, 40 );// Set position and width/heigth.
24 |
25 | contentPane.add( label );
26 | contentPane.add( button );
27 |
28 | f.getContentPane( ).add( contentPane );
29 |
30 | f.setSize( 400, 450 );// 400 width and 450 height
31 | f.setLayout( null );// using no layout managers
32 |
33 | f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Enable Close Button in window
34 |
35 | f.setVisible( true );// making the frame visible
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/swing/SwingWindowExample.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import javax.swing.JButton;
4 | import javax.swing.JFrame;
5 |
6 | public class SwingWindowExample {
7 | public static void main( String[] args ) {
8 |
9 | JFrame f = new JFrame( );// creating instance of JFrame
10 |
11 | JButton button = new JButton( "Click" );// creating instance of JButton
12 | button.setBounds( 130, 100, 100, 40 );// Set position and width/heigth.
13 |
14 | f.add( button );// adding button in JFrame
15 | f.setSize( 400, 450 );// 400 width and 450 height
16 | f.setLayout( null );// using no layout managers
17 |
18 | f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Enable Close Button in window
19 |
20 | f.setVisible( true );// making the frame visible
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/swing/SwingWindowsBuilderTest.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.EventQueue;
5 |
6 | import javax.swing.ButtonGroup;
7 | import javax.swing.JFrame;
8 | import javax.swing.JOptionPane;
9 | import javax.swing.JPanel;
10 | import javax.swing.SwingUtilities;
11 | import javax.swing.border.EmptyBorder;
12 | import javax.swing.JLabel;
13 |
14 | import java.awt.Font;
15 | import java.awt.Color;
16 | import java.awt.event.ActionEvent;
17 | import java.awt.event.ActionListener;
18 | import java.awt.event.FocusAdapter;
19 | import java.awt.event.FocusEvent;
20 | import java.awt.event.FocusListener;
21 |
22 | import javax.swing.JTextField;
23 | import javax.swing.JButton;
24 |
25 | import java.awt.Window.Type;
26 |
27 | import javax.swing.JRadioButton;
28 | import javax.swing.border.BevelBorder;
29 | import javax.swing.border.TitledBorder;
30 | import javax.swing.JCheckBox;
31 | import javax.swing.JComboBox;
32 | import javax.swing.DefaultComboBoxModel;
33 | import javax.swing.JPasswordField;
34 | import javax.swing.JSeparator;
35 |
36 | public class SwingWindowsBuilderTest extends JFrame {
37 |
38 | private JPanel contentPane;
39 | private JLabel nameLabel;
40 | private JTextField nameText;
41 | private JButton btnOk;
42 | private JLabel statusLabel;
43 | private JRadioButton rdbtnMale;
44 | private JRadioButton rdbtnFemale;
45 | private JPanel panel;
46 | private JCheckBox chckbxAge;
47 | private JComboBox comboBox;
48 | private JPasswordField passwordField;
49 | private JSeparator separator;
50 |
51 | /**
52 | * Launch the application.
53 | */
54 | public static void main( String[] args ) {
55 | SwingUtilities.invokeLater( new Runnable( ) {
56 | public void run( ) {
57 | try {
58 | SwingWindowsBuilderTest frame = new SwingWindowsBuilderTest( );
59 | frame.setVisible( true );
60 | } catch ( Exception e ) {
61 | e.printStackTrace( );
62 | }
63 | }
64 | } );
65 | SwingWindowsBuilderTest frame = new SwingWindowsBuilderTest( );
66 | frame.setVisible( true );
67 | }
68 |
69 | /**
70 | * Create the frame.
71 | */
72 | public SwingWindowsBuilderTest( ) {
73 | setTitle( "JavaStud" );
74 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
75 | setBounds( 100, 100, 480, 340 );
76 | contentPane = new JPanel( );
77 | contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
78 | setContentPane( contentPane );
79 | contentPane.setLayout( null );
80 | contentPane.add( getNameLabel( ) );
81 | contentPane.add( getNameText( ) );
82 | contentPane.add( getBtnOk( ) );
83 | contentPane.add( getStatusLabel( ) );
84 | contentPane.add( getPanel( ) );
85 | contentPane.add(getChckbxAge());
86 | contentPane.add(getComboBox());
87 | contentPane.add(getPasswordField());
88 | contentPane.add(getSeparator());
89 |
90 |
91 | }
92 |
93 | private JLabel getNameLabel( ) {
94 | if ( nameLabel == null ) {
95 | nameLabel = new JLabel( "Write Your Name" );
96 | nameLabel.setForeground( Color.BLUE );
97 | nameLabel.setFont( new Font( "Tahoma", Font.BOLD, 12 ) );
98 | nameLabel.setBounds( 31, 62, 112, 34 );
99 | }
100 | return nameLabel;
101 | }
102 |
103 | private JTextField getNameText( ) {
104 | if ( nameText == null ) {
105 | nameText = new JTextField( );
106 | nameText.setBounds( 153, 70, 150, 20 );
107 | nameText.setColumns( 10 );
108 | nameText.addFocusListener( new FocusAdapter( ) {
109 |
110 | @Override
111 | public void focusLost( FocusEvent e ) {
112 | statusLabel.setText( "Focus Lost: " + nameText.getText( ) );
113 | }
114 |
115 | @Override
116 | public void focusGained( FocusEvent e ) {
117 | statusLabel.setText( "Focus Gained: " + nameText.getText( ) );
118 | }
119 |
120 | } );
121 | }
122 | return nameText;
123 | }
124 |
125 | private JButton getBtnOk( ) {
126 | if ( btnOk == null ) {
127 | btnOk = new JButton( "OK" );
128 | btnOk.setFont( new Font( "Tahoma", Font.BOLD, 11 ) );
129 | btnOk.setBounds( 309, 69, 89, 23 );
130 | btnOk.addActionListener( new ActionListener( ) {
131 |
132 | @Override
133 | public void actionPerformed( ActionEvent e ) {
134 | String msg = " Your Name is: " + nameText.getText( );
135 | JOptionPane.showMessageDialog( contentPane, msg );
136 |
137 | }
138 | } );
139 | }
140 | return btnOk;
141 | }
142 |
143 | private JLabel getStatusLabel( ) {
144 | if ( statusLabel == null ) {
145 | statusLabel = new JLabel( "Status:" );
146 | statusLabel.setBounds( 31, 252, 308, 23 );
147 | }
148 | return statusLabel;
149 | }
150 |
151 | private JRadioButton getRdbtnMale( ) {
152 | if ( rdbtnMale == null ) {
153 | rdbtnMale = new JRadioButton( "Male" );
154 | rdbtnMale.setBounds( 6, 20, 55, 23 );
155 | }
156 | return rdbtnMale;
157 | }
158 |
159 | private JRadioButton getRdbtnFemale( ) {
160 | if ( rdbtnFemale == null ) {
161 | rdbtnFemale = new JRadioButton( "Female" );
162 | rdbtnFemale.setBounds( 65, 20, 82, 23 );
163 | }
164 | return rdbtnFemale;
165 | }
166 |
167 | private JPanel getPanel( ) {
168 | if ( panel == null ) {
169 | panel = new JPanel( );
170 | panel.setBorder( new TitledBorder( null, "Sex", TitledBorder.LEADING, TitledBorder.TOP, null, null ) );
171 | panel.setBounds( 41, 107, 164, 50 );
172 | panel.setLayout( null );
173 |
174 | ButtonGroup sexGrp = new ButtonGroup( );
175 | sexGrp.add( getRdbtnMale( ) );
176 | sexGrp.add( getRdbtnFemale( ) );
177 |
178 | panel.add( getRdbtnMale( ) );
179 | panel.add( getRdbtnFemale( ) );
180 | }
181 | return panel;
182 | }
183 | private JCheckBox getChckbxAge() {
184 | if (chckbxAge == null) {
185 | chckbxAge = new JCheckBox("Age");
186 | chckbxAge.setBounds(41, 189, 64, 23);
187 | }
188 | return chckbxAge;
189 | }
190 | private JComboBox getComboBox() {
191 | if (comboBox == null) {
192 | comboBox = new JComboBox();
193 | comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select Age Group", "5-10", "10-20", "20-30", "30-60"}));
194 | comboBox.setBounds(138, 190, 150, 34);
195 | }
196 | return comboBox;
197 | }
198 | private JPasswordField getPasswordField() {
199 | if (passwordField == null) {
200 | passwordField = new JPasswordField();
201 | passwordField.setEchoChar('*');
202 | passwordField.setBounds(239, 107, 150, 34);
203 | }
204 | return passwordField;
205 | }
206 | private JSeparator getSeparator() {
207 | if (separator == null) {
208 | separator = new JSeparator();
209 | separator.setBounds(0, 102, 465, 4);
210 | }
211 | return separator;
212 | }
213 | }
214 |
--------------------------------------------------------------------------------
/src/swing/WindowsBuilderFirst.java:
--------------------------------------------------------------------------------
1 | package swing;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.EventQueue;
5 |
6 | import javax.swing.JFrame;
7 | import javax.swing.JPanel;
8 | import javax.swing.border.EmptyBorder;
9 |
10 | public class WindowsBuilderFirst extends JFrame {
11 |
12 | private JPanel contentPane;
13 |
14 | /**
15 | * Launch the application.
16 | */
17 | public static void main( String[] args ) {
18 | EventQueue.invokeLater( new Runnable( ) {
19 | public void run( ) {
20 | try {
21 | WindowsBuilderFirst frame = new WindowsBuilderFirst( );
22 | frame.setVisible( true );
23 | } catch ( Exception e ) {
24 | e.printStackTrace( );
25 | }
26 | }
27 | } );
28 | }
29 |
30 | /**
31 | * Create the frame.
32 | */
33 | public WindowsBuilderFirst( ) {
34 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
35 | setBounds( 100, 100, 450, 300 );
36 | contentPane = new JPanel( );
37 | contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
38 | contentPane.setLayout( new BorderLayout( 0, 0 ) );
39 | setContentPane( contentPane );
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/swing/proj/LoginScreenLayout.java:
--------------------------------------------------------------------------------
1 | package swing.proj;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.Color;
5 | import java.awt.EventQueue;
6 | import java.awt.FlowLayout;
7 | import java.awt.Font;
8 | import java.awt.event.ActionEvent;
9 | import java.awt.event.ActionListener;
10 |
11 | import javax.swing.JButton;
12 | import javax.swing.JFrame;
13 | import javax.swing.JLabel;
14 | import javax.swing.JPanel;
15 | import javax.swing.JTextField;
16 | import javax.swing.UIManager;
17 | import javax.swing.border.EmptyBorder;
18 | import javax.swing.border.EtchedBorder;
19 | import javax.swing.border.TitledBorder;
20 |
21 | import com.jgoodies.forms.factories.FormFactory;
22 | import com.jgoodies.forms.layout.ColumnSpec;
23 | import com.jgoodies.forms.layout.FormLayout;
24 | import com.jgoodies.forms.layout.RowSpec;
25 |
26 | import java.awt.GridLayout;
27 | import java.sql.Connection;
28 | import java.sql.ResultSet;
29 | import java.sql.SQLException;
30 | import java.sql.Statement;
31 |
32 | import javax.swing.JPasswordField;
33 |
34 | import jdbc.MysqlConnection;
35 |
36 | public class LoginScreenLayout extends JFrame {
37 |
38 | private static final long serialVersionUID = 988172114635610780L;
39 | private JPanel contentPane;
40 | private JPanel panelTop;
41 | private JPanel panelBottom;
42 | private JPanel panelLeft;
43 | private JPanel panelRight;
44 | private JPanel panelCenter;
45 | private JLabel lblUserName;
46 | private JTextField username;
47 | private JLabel lblPassword;
48 | private JPasswordField password;
49 | private JButton btnSignIn;
50 | private JButton btnCancel;
51 | private JPanel loginPanel;
52 | private JLabel status;
53 |
54 | /**
55 | * Launch the application.
56 | */
57 | public static void main( String[] args ) {
58 | EventQueue.invokeLater( new Runnable( ) {
59 | public void run( ) {
60 | try {
61 | LoginScreenLayout loginWindow = new LoginScreenLayout( );
62 | WindowManager.ui.put( "LoginScreenLayout", loginWindow );
63 | loginWindow.setVisible( true );
64 | } catch ( Exception e ) {
65 | e.printStackTrace( );
66 | }
67 | }
68 | } );
69 | }
70 |
71 | /**
72 | * Create the frame.
73 | */
74 | public LoginScreenLayout( ) {
75 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
76 | setBounds( 100, 100, 499, 261 );
77 | contentPane = new JPanel( );
78 | contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
79 | setContentPane( contentPane );
80 | contentPane.setLayout( new BorderLayout( -2, -2 ) );
81 | contentPane.add( getPanel_1( ), BorderLayout.NORTH );
82 | contentPane.add( getPanel_1_1( ), BorderLayout.SOUTH );
83 | contentPane.add( getPanelLeft( ), BorderLayout.WEST );
84 | contentPane.add( getPanelRight( ), BorderLayout.EAST );
85 | contentPane.add( getPanelCenter( ), BorderLayout.CENTER );
86 | }
87 |
88 | private JPanel getPanel_1( ) {
89 | if ( panelTop == null ) {
90 | panelTop = new JPanel( );
91 | FlowLayout flowLayout = ( FlowLayout ) panelTop.getLayout( );
92 | flowLayout.setVgap( 10 );
93 | flowLayout.setHgap( 10 );
94 | panelTop.setBackground( UIManager.getColor( "activeCaption" ) );
95 | panelTop.setForeground( Color.BLACK );
96 | panelTop.setBorder( null );
97 | }
98 | return panelTop;
99 | }
100 |
101 | private JPanel getPanel_1_1( ) {
102 | if ( panelBottom == null ) {
103 | panelBottom = new JPanel( );
104 | FlowLayout flowLayout = ( FlowLayout ) panelBottom.getLayout( );
105 | flowLayout.setVgap( 10 );
106 | flowLayout.setHgap( 10 );
107 | panelBottom.setBackground( UIManager.getColor( "activeCaption" ) );
108 | panelBottom.setForeground( Color.BLACK );
109 | }
110 | return panelBottom;
111 | }
112 |
113 | private JPanel getPanelLeft( ) {
114 | if ( panelLeft == null ) {
115 | panelLeft = new JPanel( );
116 | FlowLayout flowLayout = ( FlowLayout ) panelLeft.getLayout( );
117 | flowLayout.setVgap( 10 );
118 | flowLayout.setHgap( 10 );
119 | panelLeft.setBackground( UIManager.getColor( "activeCaption" ) );
120 | panelLeft.setForeground( Color.BLACK );
121 | }
122 | return panelLeft;
123 | }
124 |
125 | private JPanel getPanelRight( ) {
126 | if ( panelRight == null ) {
127 | panelRight = new JPanel( );
128 | FlowLayout flowLayout = ( FlowLayout ) panelRight.getLayout( );
129 | flowLayout.setVgap( 10 );
130 | flowLayout.setHgap( 10 );
131 | panelRight.setBackground( UIManager.getColor( "activeCaption" ) );
132 | panelRight.setForeground( Color.BLACK );
133 | }
134 | return panelRight;
135 | }
136 |
137 | private JPanel getPanelCenter( ) {
138 | if ( panelCenter == null ) {
139 | panelCenter = new JPanel( );
140 | panelCenter.setBackground( new Color( 204, 255, 204 ) );
141 | panelCenter.setBorder( new EtchedBorder( EtchedBorder.LOWERED, null, null ) );
142 | panelCenter.setForeground( Color.BLACK );
143 | panelCenter.setLayout( new FormLayout( new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode( "default:grow" ), ColumnSpec.decode( "6dlu" ), FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode( "default:grow" ), }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode( "default:grow" ), FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, } ) );
144 | panelCenter.add( getLoginPanel( ), "2, 2, 9, 8, fill, fill" );
145 | panelCenter.add( getStatus( ), "8, 10, 3, 1" );
146 | panelCenter.add( getBtnSignIn( ), "4, 14" );
147 | panelCenter.add( getBtnCancel( ), "10, 14" );
148 | }
149 | return panelCenter;
150 | }
151 |
152 | private JLabel getLblUserName( ) {
153 | if ( lblUserName == null ) {
154 | lblUserName = new JLabel( "User Name:" );
155 | lblUserName.setFont( new Font( "Tahoma", Font.BOLD, 13 ) );
156 | }
157 | return lblUserName;
158 | }
159 |
160 | private JTextField getUsername( ) {
161 | if ( username == null ) {
162 | username = new JTextField( );
163 | username.setSize( 250, 30 );
164 | }
165 | return username;
166 | }
167 |
168 | private JLabel getLblPassword( ) {
169 | if ( lblPassword == null ) {
170 | lblPassword = new JLabel( "Password:" );
171 | lblPassword.setFont( new Font( "Tahoma", Font.BOLD, 13 ) );
172 | }
173 | return lblPassword;
174 | }
175 |
176 | private JPasswordField getPassword( ) {
177 | if ( password == null ) {
178 | password = new JPasswordField( );
179 | password.setEchoChar( '*' );
180 | }
181 | return password;
182 | }
183 |
184 | private JButton getBtnSignIn( ) {
185 | if ( btnSignIn == null ) {
186 | btnSignIn = new JButton( "Sign In" );
187 | btnSignIn.setFont( new Font( "Tahoma", Font.BOLD, 11 ) );
188 | btnSignIn.addActionListener( new ActionListener( ) {
189 |
190 | @Override
191 | public void actionPerformed( ActionEvent e ) {
192 | try {
193 | Connection conn = MysqlConnection.getConnection( );
194 | Statement stmt = conn.createStatement( );
195 | ResultSet rs = stmt.executeQuery( "select * from student" );
196 | while ( rs.next( ) ) {// Loop Each Row
197 | // Fetch columns
198 | System.out.println( rs.getInt( 1 ) + " " + rs.getString( 2 ) );
199 | }
200 | conn.close( );
201 | } catch ( ClassNotFoundException | SQLException e1 ) {
202 | e1.printStackTrace( );
203 | }
204 |
205 | switchToStudMangScreen( );
206 | }
207 | } );
208 | }
209 | return btnSignIn;
210 | }
211 |
212 | private JButton getBtnCancel( ) {
213 | if ( btnCancel == null ) {
214 | btnCancel = new JButton( "Cancel" );
215 | btnCancel.addActionListener( new ActionListener( ) {
216 | @Override
217 | public void actionPerformed( ActionEvent e ) {
218 | WindowManager.ui.clear( );
219 | System.exit( 0 );
220 | }
221 | } );
222 | }
223 | return btnCancel;
224 | }
225 |
226 | private JPanel getLoginPanel( ) {
227 | if ( loginPanel == null ) {
228 | loginPanel = new JPanel( );
229 | loginPanel.setBorder( new TitledBorder( UIManager.getBorder( "TitledBorder.border" ), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color( 0, 0, 0 ) ) );
230 | loginPanel.setLayout( new GridLayout( 2, 3, 2, 2 ) );
231 | loginPanel.add( getLblUserName( ) );
232 | loginPanel.add( getUsername( ) );
233 | loginPanel.add( getLblPassword( ) );
234 | loginPanel.add( getPassword( ) );
235 | }
236 | return loginPanel;
237 | }
238 |
239 | private JLabel getStatus( ) {
240 | if ( status == null ) {
241 | status = new JLabel( "Status:" );
242 | status.setForeground( new Color( 204, 0, 102 ) );
243 | status.setFont( new Font( "Tahoma", Font.BOLD, 11 ) );
244 | }
245 | return status;
246 | }
247 |
248 | private void switchToStudMangScreen( ) {
249 | // Open next Student Window
250 | StudentMangement studMngWindow = new StudentMangement( username.getText( ) );
251 | WindowManager.ui.put( "StudentMangement", studMngWindow );
252 | studMngWindow.setVisible( true );
253 |
254 | LoginScreenLayout loginWindow = ( LoginScreenLayout ) WindowManager.ui.get( "LoginScreenLayout" );
255 | loginWindow.clearData( );
256 | loginWindow.setVisible( false );
257 |
258 | }
259 |
260 | private void clearData( ) {
261 | username.setText( "" );
262 | password.setText( "" );
263 | }
264 | }
265 |
--------------------------------------------------------------------------------
/src/swing/proj/WindowManager.java:
--------------------------------------------------------------------------------
1 | package swing.proj;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class WindowManager {
7 | public static Map< String, Object > ui = new HashMap< String, Object >( );
8 | }
9 |
--------------------------------------------------------------------------------
/src/test/java/JButtonTableExample.java:
--------------------------------------------------------------------------------
1 | package test.java;
2 |
3 | import java.awt.Component;
4 | import java.awt.event.ActionEvent;
5 | import java.awt.event.ActionListener;
6 | import java.awt.event.WindowAdapter;
7 | import java.awt.event.WindowEvent;
8 |
9 | import javax.swing.DefaultCellEditor;
10 | import javax.swing.JButton;
11 | import javax.swing.JCheckBox;
12 | import javax.swing.JFrame;
13 | import javax.swing.JOptionPane;
14 | import javax.swing.JScrollPane;
15 | import javax.swing.JTable;
16 | import javax.swing.UIManager;
17 | import javax.swing.table.DefaultTableModel;
18 | import javax.swing.table.TableCellRenderer;
19 |
20 | /**
21 | * @version 1.0 11/09/98
22 | */
23 | public class JButtonTableExample extends JFrame {
24 |
25 | public JButtonTableExample( ) {
26 | super( "JButtonTable Example" );
27 |
28 | DefaultTableModel dm = new DefaultTableModel( );
29 | dm.setDataVector( new Object[][] { { "button 1", "foo" }, { "button 2", "bar" } }, new Object[] { "Button", "String" } );
30 |
31 | // JTable table = new JTable( new Object[][] { { "button 1", "foo" }, { "button 2", "bar" } }, new Object[] { "Button", "String" } );
32 | JTable table = new JTable( dm );
33 | table.getColumn( "Button" ).setCellRenderer( new ButtonRenderer( ) );
34 | table.getColumn( "Button" ).setCellEditor( new ButtonEditor( new JCheckBox( ) ) );
35 | JScrollPane scroll = new JScrollPane( table );
36 | getContentPane( ).add( scroll );
37 | setSize( 400, 100 );
38 | setVisible( true );
39 | }
40 |
41 | public static void main( String[] args ) {
42 |
43 | JButtonTableExample frame = new JButtonTableExample( );
44 | frame.addWindowListener( new WindowAdapter( ) {
45 | public void windowClosing( WindowEvent e ) {
46 | System.exit( 0 );
47 | }
48 | } );
49 | }
50 | }
51 |
52 | /**
53 | * @version 1.0 11/09/98
54 | */
55 |
56 | class ButtonRenderer extends JButton implements TableCellRenderer {
57 |
58 | public ButtonRenderer( ) {
59 | setOpaque( true );
60 | }
61 |
62 | public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
63 | if ( isSelected ) {
64 | setForeground( table.getSelectionForeground( ) );
65 | setBackground( table.getSelectionBackground( ) );
66 | } else {
67 | setForeground( table.getForeground( ) );
68 | setBackground( UIManager.getColor( "Button.background" ) );
69 | }
70 | setText( ( value == null ) ? "" : value.toString( ) );
71 | return this;
72 | }
73 | }
74 |
75 | /**
76 | * @version 1.0 11/09/98
77 | */
78 |
79 | class ButtonEditor extends DefaultCellEditor {
80 | protected JButton button;
81 |
82 | private String label;
83 |
84 | private boolean isPushed;
85 |
86 | public ButtonEditor( JCheckBox checkBox ) {
87 | super( checkBox );
88 | button = new JButton( );
89 | button.setOpaque( true );
90 | button.addActionListener( new ActionListener( ) {
91 | public void actionPerformed( ActionEvent e ) {
92 | fireEditingStopped( );
93 | }
94 | } );
95 | }
96 |
97 | public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column ) {
98 | if ( isSelected ) {
99 | button.setForeground( table.getSelectionForeground( ) );
100 | button.setBackground( table.getSelectionBackground( ) );
101 | } else {
102 | button.setForeground( table.getForeground( ) );
103 | button.setBackground( table.getBackground( ) );
104 | }
105 | label = ( value == null ) ? "" : value.toString( );
106 | button.setText( label );
107 | isPushed = true;
108 | return button;
109 | }
110 |
111 | public Object getCellEditorValue( ) {
112 | if ( isPushed ) {
113 | //
114 | //
115 | JOptionPane.showMessageDialog( button, label + ": Ouch!" );
116 | // System.out.println(label + ": Ouch!");
117 | }
118 | isPushed = false;
119 | return new String( label );
120 | }
121 |
122 | public boolean stopCellEditing( ) {
123 | isPushed = false;
124 | return super.stopCellEditing( );
125 | }
126 |
127 | protected void fireEditingStopped( ) {
128 | super.fireEditingStopped( );
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/src/test/java/SpinnerTest.java:
--------------------------------------------------------------------------------
1 | package test.java;
2 |
3 | import java.awt.EventQueue;
4 |
5 | import javax.swing.JFrame;
6 | import javax.swing.JPanel;
7 | import javax.swing.JSpinner;
8 |
9 | public class SpinnerTest {
10 |
11 | private JFrame frame;
12 | private JPanel panel;
13 | private JSpinner spinner;
14 |
15 | /**
16 | * Launch the application.
17 | */
18 | public static void main( String[] args ) {
19 | EventQueue.invokeLater( new Runnable( ) {
20 | public void run( ) {
21 | try {
22 | SpinnerTest window = new SpinnerTest( );
23 | window.frame.setVisible( true );
24 | } catch ( Exception e ) {
25 | e.printStackTrace( );
26 | }
27 | }
28 | } );
29 | }
30 |
31 | /**
32 | * Create the application.
33 | */
34 | public SpinnerTest( ) {
35 | initialize( );
36 | }
37 |
38 | /**
39 | * Initialize the contents of the frame.
40 | */
41 | private void initialize( ) {
42 | frame = new JFrame( );
43 | frame.setBounds( 100, 100, 450, 300 );
44 | frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
45 | frame.getContentPane( ).setLayout( null );
46 | frame.getContentPane( ).add( getPanel( ) );
47 | }
48 |
49 | private JPanel getPanel( ) {
50 | if ( panel == null ) {
51 | panel = new JPanel( );
52 | panel.setBounds( 10, 11, 414, 240 );
53 | panel.setLayout( null );
54 | panel.add( getSpinner( ) );
55 | }
56 | return panel;
57 | }
58 |
59 | private JSpinner getSpinner( ) {
60 | if ( spinner == null ) {
61 | spinner = new JSpinner( );
62 | spinner.setBounds( 45, 28, 193, 77 );
63 | }
64 | return spinner;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/test/java/Tab.java:
--------------------------------------------------------------------------------
1 | package test.java;
2 |
3 | import java.awt.EventQueue;
4 |
5 | import javax.swing.JFrame;
6 | import javax.swing.JTable;
7 | import javax.swing.table.TableColumn;
8 | import javax.swing.text.TableView.TableRow;
9 |
10 | public class Tab {
11 |
12 | private JFrame frame;
13 | private JTable table;
14 |
15 | /**
16 | * Launch the application.
17 | */
18 | public static void main( String[] args ) {
19 | EventQueue.invokeLater( new Runnable( ) {
20 | public void run( ) {
21 | try {
22 | Tab window = new Tab( );
23 | window.frame.setVisible( true );
24 | } catch ( Exception e ) {
25 | e.printStackTrace( );
26 | }
27 | }
28 | } );
29 | }
30 |
31 | /**
32 | * Create the application.
33 | */
34 | public Tab( ) {
35 | initialize( );
36 | }
37 |
38 | /**
39 | * Initialize the contents of the frame.
40 | */
41 | private void initialize( ) {
42 | frame = new JFrame( );
43 | frame.setBounds( 100, 100, 450, 300 );
44 | frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
45 | frame.getContentPane( ).setLayout( null );
46 | frame.getContentPane( ).add( getTable( ) );
47 | }
48 |
49 | private JTable getTable( ) {
50 | if ( table == null ) {
51 |
52 | String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
53 | Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer( 5 ), new Boolean( false ) }, { "John", "Doe", "Rowing", new Integer( 3 ), new Boolean( true ) }, { "Sue", "Black", "Knitting", new Integer( 2 ), new Boolean( false ) }, { "Jane", "White", "Speed reading", new Integer( 20 ), new Boolean( true ) }, { "Joe", "Brown", "Pool", new Integer( 10 ), new Boolean( false ) } };
54 |
55 | table = new JTable( data, columnNames );
56 | table.setBounds( 27, 32, 397, 203 );
57 |
58 | // TableColumn tcol = new TableColumn( );
59 | // tcol.setHeaderValue( "Id" );
60 | // tcol.setHeaderValue( "Name" );
61 | // tcol.setHeaderValue( "Address" );
62 |
63 | // TableRow rowI = new TableRow( elem );
64 |
65 | }
66 | return table;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/test/java/TableTest.java:
--------------------------------------------------------------------------------
1 | package test.java;
2 |
3 | import java.awt.EventQueue;
4 | import java.util.Vector;
5 | import javax.swing.JFrame;
6 | import javax.swing.JScrollPane;
7 | import javax.swing.JTable;
8 | import javax.swing.UIManager;
9 | import javax.swing.UnsupportedLookAndFeelException;
10 | import javax.swing.table.DefaultTableModel;
11 |
12 | public class TableTest {
13 |
14 | public static void main( String[] args ) {
15 | new TableTest( );
16 | }
17 |
18 | public TableTest( ) {
19 | startUI( );
20 | }
21 |
22 | public void startUI( ) {
23 | EventQueue.invokeLater( new Runnable( ) {
24 | @Override
25 | public void run( ) {
26 | try {
27 | UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName( ) );
28 | } catch ( ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex ) {
29 | ex.printStackTrace( );
30 | }
31 |
32 | MyTableModel model = new MyTableModel( );
33 | model.addRow( new Object[] { 0, "Brian", false } );
34 | model.addRow( new Object[] { 1, "Ned", false } );
35 | model.addRow( new Object[] { 2, "John", false } );
36 | model.addRow( new Object[] { 3, "Drogo", false } );
37 | JTable table = new JTable( model );
38 |
39 | JFrame frame = new JFrame( "Testing" );
40 | frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
41 | frame.add( new JScrollPane( table ) );
42 | frame.pack( );
43 | frame.setLocationRelativeTo( null );
44 | frame.setVisible( true );
45 | }
46 | } );
47 | }
48 |
49 | public class MyTableModel extends DefaultTableModel {
50 |
51 | public MyTableModel( ) {
52 | super( new String[] { "ID", "Name", "Present" }, 0 );
53 | }
54 |
55 | @Override
56 | public Class< ? > getColumnClass( int columnIndex ) {
57 | Class clazz = String.class;
58 | switch ( columnIndex ) {
59 | case 0:
60 | clazz = Integer.class;
61 | break;
62 | case 2:
63 | clazz = Boolean.class;
64 | break;
65 | }
66 | return clazz;
67 | }
68 |
69 | @Override
70 | public boolean isCellEditable( int row, int column ) {
71 | return column == 2;
72 | }
73 |
74 | @Override
75 | public void setValueAt( Object aValue, int row, int column ) {
76 | if ( aValue instanceof Boolean && column == 2 ) {
77 | System.out.println( aValue );
78 | Vector rowData = ( Vector ) getDataVector( ).get( row );
79 | rowData.set( 2, ( boolean ) aValue );
80 | fireTableCellUpdated( row, column );
81 | }
82 | }
83 |
84 | }
85 |
86 | }
--------------------------------------------------------------------------------
/src/test/java/TableWithSpinner.java:
--------------------------------------------------------------------------------
1 | package test.java;
2 |
3 | import java.awt.Component;
4 | import java.awt.EventQueue;
5 |
6 | import javax.swing.JFrame;
7 | import javax.swing.JPanel;
8 | import javax.swing.JSpinner;
9 | import javax.swing.JTable;
10 | import javax.swing.table.DefaultTableModel;
11 | import javax.swing.table.TableCellEditor;
12 | import javax.swing.table.TableCellRenderer;
13 | import javax.swing.JScrollPane;
14 |
15 | public class TableWithSpinner {
16 |
17 | private JFrame frame;
18 | private JTable table;
19 | private DefaultTableModel model;
20 | private JPanel panel;
21 | private JScrollPane scrollPane;
22 | private JSpinner spinner;
23 |
24 | /**
25 | * Launch the application.
26 | */
27 | public static void main( String[] args ) {
28 | EventQueue.invokeLater( new Runnable( ) {
29 | public void run( ) {
30 | try {
31 | TableWithSpinner window = new TableWithSpinner( );
32 | window.frame.setVisible( true );
33 | // window.getTableModel( ).setColumnCount( 0 );
34 | } catch ( Exception e ) {
35 | e.printStackTrace( );
36 | }
37 | }
38 | } );
39 | }
40 |
41 | /**
42 | * Create the application.
43 | */
44 | public TableWithSpinner( ) {
45 | initialize( );
46 | }
47 |
48 | /**
49 | * Initialize the contents of the frame.
50 | */
51 | private void initialize( ) {
52 | frame = new JFrame( );
53 | frame.setBounds( 100, 100, 450, 300 );
54 | frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
55 | frame.getContentPane( ).setLayout( null );
56 | frame.getContentPane( ).add( getPanel( ) );
57 | }
58 |
59 | private JTable getTable( ) {
60 | if ( table == null ) {
61 | table = new JTable( getTableModel( ) );
62 | }
63 | return table;
64 | }
65 |
66 | private DefaultTableModel getTableModel( ) {
67 | if ( model == null ) {
68 | model = new DefaultTableModel( );
69 | model.setDataVector( new Object[][] { { "", "", "" } }, new Object[] { "ID", "NAME", "ADDRESS" } );
70 | }
71 | return model;
72 | }
73 |
74 | private JPanel getPanel( ) {
75 | if ( panel == null ) {
76 | panel = new JPanel( );
77 | panel.setBounds( 10, 11, 414, 240 );
78 | panel.setLayout( null );
79 | panel.add( getScrollPane( ) );
80 | }
81 | return panel;
82 | }
83 |
84 | private JScrollPane getScrollPane( ) {
85 | if ( scrollPane == null ) {
86 | scrollPane = new JScrollPane( );
87 | scrollPane.setBounds( 10, 11, 394, 218 );
88 | scrollPane.setViewportView( getTable( ) );
89 | }
90 | return scrollPane;
91 | }
92 |
93 | private JSpinner getSpinner( ) {
94 | if ( spinner == null ) {
95 | spinner = new JSpinner( );
96 | spinner.setBounds( 45, 28, 193, 77 );
97 | }
98 | return spinner;
99 | }
100 |
101 | class JSpinnerRenderer extends JSpinner implements TableCellRenderer {
102 | JSpinnerRenderer( ) {
103 | setOpaque( true );
104 | }
105 |
106 | @Override
107 | public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
108 | return this;
109 | }
110 |
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/src/util/CalculatorConsole.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Sample Calculator without precedence check.
7 | *
8 | * @author ojhay
9 | *
10 | */
11 | public class CalculatorConsole {
12 | public static void main( String[] args ) {
13 |
14 | Scanner sc = new Scanner( System.in );
15 |
16 | System.out.println( "Calculator:" );
17 | while ( true ) {
18 |
19 | System.out.print( "Type arithmetic Expression: " );
20 | String expression = sc.nextLine( );
21 |
22 | // Exit if user input: exit
23 | if ( expression.equalsIgnoreCase( "exit" ) ) {
24 | break;
25 | }
26 |
27 | double result = expre( expression );
28 | System.out.println( expression + "= " + result );
29 |
30 | }
31 | sc.close( );
32 | }
33 |
34 | // Recursive method to evaluate arithmetic expression.
35 | public static double expre( String expre ) {
36 |
37 | if ( ! ( expre.contains( "+" ) || expre.contains( "-" ) || expre.contains( "*" ) || expre.contains( "/" ) ) ) {
38 | return Integer.valueOf( expre );
39 | }
40 |
41 | double result = 0;
42 | for ( int i = 0; i < expre.length( ); i++ ) {
43 |
44 | char symbol = expre.charAt( i );
45 | if ( !Character.isDigit( symbol ) ) { // Symbol
46 |
47 | double operand1 = Integer.parseInt( expre.substring( 0, i ) );
48 |
49 | switch ( symbol ) {
50 | case '+':
51 | result = operand1 + expre( expre.substring( i + 1 ) );
52 | break;
53 | case '-':
54 | result = operand1 - expre( expre.substring( i + 1 ) );
55 | break;
56 | case '*':
57 | result = operand1 * expre( expre.substring( i + 1 ) );
58 | break;
59 | case '/':
60 | result = operand1 / expre( expre.substring( i + 1 ) );
61 | break;
62 | }
63 |
64 | break;
65 | }
66 | }
67 | return result;
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------