1. ArrayList
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Zoloo");
list1.add("Bataa");
list1.add("Miigaa");
list1.add("Hurlee");
String input = JOptionPane.showInputDialog("Please enter name:");
list1.add(input);
String msg="";
for(String s: list1) msg+=s + "\n";
JOptionPane.showMessageDialog(null, msg);
}
2. Reading and writing files
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String[] array = new String[]{"Carmelo Anthony",
"Lebron James", "Kevin Durant",
"Paul Pierce", "Paul George" };
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
for(String s : array)
bw.write(s + "\n");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String s = "";
String msg= "";
while((s = br.readLine())!=null)
msg += s + "\n";
br.close();
JOptionPane.showMessageDialog(null, msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3. Map and HashMap
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Carmelo", 15);
map.put("Lebron", 23);
map.put("Kobe", 24);
map.put("Duncan", 21);
map.put("Dirk", 41);
int markofKobe = map.get("Kobe");
System.out.println("Marks of Kobe :" + markofKobe);
int markofDuncan = map.get("Duncan");
System.out.println("Marks of Duncan :" + markofDuncan);
map.remove("Kobe");
if(map.containsKey("Kobe"))
System.out.println("Kobe exists!");
else System.out.println("Kobe is gone!");
for(Entry<String, Integer> s : map.entrySet())
System.out.println("Key: " + s.getKey() + " Value: " + s.getValue());
}
4. Interface Example
Interface:
public interface Person {
public String whoRyou();
public String getName();
}
Class 2:
public class GoodPerson implements Person{
String name="";
public GoodPerson(String n) {
// TODO Auto-generated constructor stub
name = n;
}
@Override
public String whoRyou() {
// TODO Auto-generated method stub
return "Good";
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
}
Class 1:
public class BadPerson implements Person{
String name="";
public BadPerson(String n) {
// TODO Auto-generated constructor stub
name = n;
}
@Override
public String whoRyou() {
// TODO Auto-generated method stub
return "Bad";
}
@Override
public String getName() {
// TODO Auto-generated method stub
return name;
}
}
Main Class:
public class Killer {
public static void main(String[] args){
GoodPerson gp1 = new GoodPerson("Percy");
GoodPerson gp2 = new GoodPerson("Harry");
BadPerson bp1 = new BadPerson("Black");
BadPerson bp2 = new BadPerson("Reddy");
kill(gp1);
kill(gp2);
kill(bp1);
kill(bp2);
}
public static void kill(Person p){
if(p instanceof BadPerson)
System.out.println("Killed " + p.getName() + ". Feels good to kill a bad person.");
else if(p instanceof GoodPerson)
System.out.println("Killed " + p.getName() + ". Cannot kill a good person.");
}
}
5. Abstraction
public abstract class Shape {
String colorname = "";
int vertices = 0;
public abstract void Draw();
public String getColorname() {
return colorname;
}
public void setColorname(String colorname) {
this.colorname = colorname;
}
public int getVertices() {
return vertices;
}
public void setVertices(int vertices) {
this.vertices = vertices;
}
}
public class Circle extends Shape{
public Circle(String n){
super.setColorname(n);
super.setVertices(0);
System.out.println("Circle has been created!");
}
@Override
public void Draw() {
// TODO Auto-generated method stub
System.out.println("Circle has been drawn");
}
}
main Class:
public static void main(String[] args){
Circle c1= new Circle("Black");
Circle c2 = new Circle("Red");
Shape c3 = new Circle("Blue");
c1.Draw();
c2.Draw();
c3.Draw();
}
https://www.youtube.com/playlist?list=PL8F0C07390BAC102A
No comments:
Post a Comment