forked from yedhink/KTU-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
395 changed files
with
183,356 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// a abstract class | ||
abstract class H { | ||
int x; | ||
|
||
/* abstract */ int y; // variables cannot be abstract | ||
|
||
/* abstract */ H () {x=1;} // constructor cannot be abstract | ||
|
||
void triple (int n) {x=x*3;}; // a normal method | ||
|
||
static int triple2 (int n) {return n*3;}; // a static method in abstract class is ok. | ||
abstract void triple3 (); // abstract method. Note: no definition. | ||
|
||
// static abstract void triple3 (int n); // abstract static cannot be combined | ||
int returnMe () {return x;} | ||
} | ||
|
||
// H1 extends (inherits) H. When a class extends a abstract class, | ||
// it is said to “implement” the abstract class. | ||
class H1 extends H { | ||
void triple3 () {x=x*3+1;} // must be defined, else compiler makes a complaint. | ||
//Also, all return type and parameter must agree with the parent class. | ||
} | ||
|
||
public class AbstractExample { | ||
public static void main(String[] args) { | ||
// H xx = new H(); // abstract class cannot be instantiated | ||
H1 myO = new H1(); // abstract class cannot be instantiated | ||
myO.triple3(); | ||
System.out.println(myO.returnMe()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## The “abstract” Keyword | ||
|
||
1. The keyword abstract can be used on class declaration. For example abstract MyClassX (…);. | ||
1. The keyword abstract can be used on method declaration. For example abstract int f (…);. | ||
1. When a class is declared abstract, it cannot be instantiated. | ||
1. When a method is declared abstract, it cannot have definition. | ||
1. Only abstract classes can have abstract methods. Abstract class does not necessarily require its methods to be all abstract. | ||
|
||
## Purpose of Abstract Classes | ||
|
||
The purpose of abstract class is to serve purely as a parent of classes. (that is, solely for the purpose of extension (inheritance) by other classes.) | ||
|
||
The difference between abstract class and “normal” class is that, the abstract class's methods cannot have any definition. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package operators; | ||
public class CommonDataTypes { | ||
/** | ||
* This program demonstrates common variable type usage and | ||
* how to format output of variables. | ||
*/ | ||
public static void main(String[] args) { | ||
String studentName = "Bette Itall"; | ||
int age = 22; | ||
double gpa = 3.75; | ||
boolean isFemale = true; | ||
System.out.printf("%s is %d years old. %s has a %4.3f gpa.\n", | ||
studentName, | ||
age, | ||
(isFemale ? "She" : "He"), | ||
gpa); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package operators; | ||
|
||
import java.util.Scanner; | ||
|
||
public class OnePlusTwoEqualsTwelve { | ||
|
||
public static void main(String[] args) { | ||
// 1 + 2 equals 12? Whaaaaaaaaaaaaat??!?!?!? | ||
String x = "1"; | ||
String y = "2"; | ||
System.out.println(x + y); // not what you expect; | ||
|
||
String first = "Jimmy "; | ||
String last = "Page"; | ||
System.out.println(first + last ); | ||
|
||
// so, how does one to convert string to int? | ||
int i = Integer.parseInt(x); | ||
int j = Integer.parseInt(y); | ||
|
||
System.out.println(i + j); | ||
|
||
// What about booleans? | ||
String areYouHappy = "true"; | ||
boolean ishappy = Boolean.parseBoolean(areYouHappy); | ||
System.out.println(ishappy); | ||
|
||
// Or String to double? | ||
String gpa = "3.96"; | ||
Double realgpa = Double.parseDouble(gpa); | ||
System.out.println(realgpa - 1.0 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package operators; | ||
|
||
public class UnderstandingTypeCasting { | ||
|
||
public static void main(String[] args) { | ||
// This demo will help explain type casting. | ||
int x = 20; | ||
int y = 15; | ||
int i = x / y; // int / int == int | ||
double d1 = x / y; // int / int == int then cast to double | ||
// implicit type cast | ||
double d2 = x / (y * 1.0); // int * double == double; int / double = double | ||
// explicit type cast | ||
double d3 = x / (double)y; // int / double = double | ||
|
||
System.out.println(i); | ||
System.out.println(d1); | ||
System.out.println(d2); | ||
System.out.println(d3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
import java.sql.*; | ||
/******************** | ||
Contributed by leo | ||
->make sure that you have JDBC Driver install. | ||
->You should have a SQL server Running. | ||
************************/ | ||
public class JdbcBeg implements Runnable{ | ||
//Threads are just used to make Server timings | ||
public void run() { | ||
System.out.print("Connecting to SQL DB waiting For Connection "); | ||
for (int i = 0; i < 5; i++) { | ||
try { | ||
Thread.sleep(1000); | ||
System.out.print("."); | ||
} catch (InterruptedException e) { | ||
System.out.println("Error while threading ."); | ||
} | ||
} | ||
System.out.println("\nYou are connected to SQL DB"); | ||
|
||
} | ||
public static void main(String[] args) { | ||
Thread thread=new Thread(new JdbcBeg()); | ||
thread.start(); | ||
try{ | ||
Thread.sleep(5000); | ||
thread.join(); | ||
}catch (InterruptedException e){ | ||
System.out.println("Something wrong while threading."); | ||
} | ||
|
||
try { | ||
//Connection is obtained using GET method. Replace the url,username,password with yours. | ||
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "nopass1234"); | ||
Statement statement = connection.createStatement(); | ||
statement.executeUpdate("use employee");//Use your update statements instead of mine. | ||
ResultSet resultSet = statement.executeQuery("select * from livesin");//Use your select queries here | ||
//Result is stored as a ResultSet object | ||
while (resultSet.next()) { | ||
System.out.println(); | ||
System.out.println(resultSet.getString("pname") + " " + resultSet.getString("street") + " " + resultSet.getString("city") + " "); | ||
//Replace the column label to columns used in the table | ||
} | ||
|
||
} catch (SQLException sqlException) { | ||
System.out.println("Error occurred while processing"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Requirements | ||
|
||
* Set up MySql | ||
* [in Windows](https://www.roseindia.net/mysql/installing-mysql-on-windows10.shtml) | ||
* [in most common Linux distros](https://dev.mysql.com/doc/refman/8.0/en/linux-installation.html) | ||
|
||
**in Arch Linux setting up both MySql and JDBC can be found [here](https://wiki.archlinux.org/index.php/JDBC_and_MySQL)** | ||
|
||
* Set up JDBC | ||
* [in Windows](https://blogs.msdn.microsoft.com/brian_swan/2011/03/02/getting-started-with-the-sql-server-jdbc-driver/) | ||
* [in Linux](https://stackoverflow.com/questions/5307048/where-do-i-install-a-jdbc-driver-on-ubuntu) | ||
|
||
## Creating your own Database in MySql (optional) | ||
|
||
After completing the installations , follow this [link](https://www.liquidweb.com/kb/create-a-mysql-database-on-linux-via-command-line/) and create a database of your own. | ||
Do this if you want to learn working with MySql. Else make use of the already created DB in the given example(s). | ||
|
||
## Usage | ||
|
||
Its important to note that , when , say you're in Arch Linux, then your default vendor is mariadb , thus in the program you would have to replace "mysql" with "mariadb". | ||
Rest of the execution as same as what had been done with other programs , compile and run `JdbcBeg.java` |
Oops, something went wrong.