Wednesday, December 9, 2015

Java Basic 1

1. Variable Length Arguments:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(avg(5,2,3,5,6,7));
System.out.println(avg(10,11,21,33));

}

public static int avg(int...numbers){
int t = 0;
for(int x: numbers)
t+=x;
return t/numbers.length;
}

2. Enumeration and EnumSet range:
public enum someClass {
bucky("nice", "22"),
kelsey("cutie", "10"),
julia("bigmistake", "12"),
bataa("cool","25"),
azaa("good", "15");

final String desc;
final String year;

someClass(String de, String b){
desc = de;
year = b;
}
public String getDesc(){
return desc;
}
public String getYear(){
return year;
}

}

mainclass:
public static void main(String[] args) {
// TODO Auto-generated method stub

for(someClass c: EnumSet.range(someClass.kelsey, someClass.bataa))
//for(someClass c: someClass.values())

System.out.printf("%s\t%s\t%s\n", c, c.desc, c.year);

}
3. Static Example:
public class someClass {
String fname;
String lname;
static int members=0;

someClass(String f, String l){
fname = f;
lname = l;
members++;
System.out.printf("\nNew Member firstname: %s, lastname: %s, Id: %d", fname, lname, members);
}
public String getFname(){
return fname;
}
public String getLname(){
return lname;
}
public int getMembers(){
return members;
}
}

mainClass:
public static void main(String[] args) {
// TODO Auto-generated method stub
someClass sc1 = new someClass("Carmelo", "Anthony");
someClass sc2 = new someClass("Lebron", "James");
someClass sc3 = new someClass("Chris", "Paul");

System.out.println("\n" + sc1.getMembers());

}


Array Holding Many Objects:
 public class someClassList {
     
      someClass[] sc = new someClass[5];
      int i = 0;
     
      public void add(someClass s){
            if(i<sc.length)
            {
                  sc[i] = s;
                  System.out.println("some class added by index: " + i);
                  i++;
                  }
            }

}
B.  public class OtherClass extends someClass{

}
C.  public class someClass{

}    
Main class:
public class func1{
     
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClassList scl = new someClassList();
            OtherClass os = new OtherClass();
            someClass sc = new someClass();
            scl.add(os);
            scl.add(sc);
           
      }
}

Simple Polymorphic:
public class someClass{
     
      public void noise(){
            System.out.println("Base class noise");
      }

}
public class OtherClass extends someClass{
      public void noise(){
            System.out.println("OtherCLass noise");
      }
}
Main Class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            OtherClass os = new OtherClass();
            os.noise();
            someClass sc = new someClass();
            sc.noise();
      }


File
Creating Files:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            final Formatter x;
            try{
                  x = new Formatter("javatutorial.txt");
                  System.out.println("You created a file!");
                 
            }catch(Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
Writing to Files:
public class someClass{
     
      private Formatter x;
      public void openFile(){
            try{
                  x = new Formatter("javaa.txt");    
            } catch (Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
      public void addRecords(){
            x.format("%s%s%s", "15", "Carmelo", "Anthony");
      }
      public void closeFile(){
            x.close();
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.openFile();
            sc.addRecords();
            sc.closeFile();
      }
Reading to Files:
 public class someClass{
      private Scanner x;
     
      public void openFile(){
            try{
                  x = new Scanner(new File("javaa.txt"));
            } catch (Exception ex){
                  System.out.println(ex.getMessage());
            }
      }
     
      public void readFile(){
            String a, b, c;
            while(x.hasNext()){
                  a = x.next();
                  b = x.next();
                  c = x.next();
                  System.out.printf("%s %s %s\n", a, b, c);
            }
      }
     
      public void closeFile(){
            x.close();
      }
}
Main class:
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            someClass sc = new someClass();
            sc.openFile();
            sc.readFile();
            sc.closeFile();

      } 

Видео линк:https://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28

No comments:

Post a Comment