Skip to content

Week7Examples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Week7/CalendarDate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import static java.util.Calendar.*;

class CalendarDate
{
public static void main(String[] args) throws java.text.ParseException
{
Calendar calendar = Calendar.getInstance();
calendar.roll( MONTH, 1);
System.out.println( calendar.get(MONTH) + "��" + calendar.get( DAY_OF_MONTH ) + "��");

Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
System.out.println( formatter.format(date ));

date = new SimpleDateFormat("yyyy-MM-dd").parse( "2013-4-23" );
calendar.setTime( date );
System.out.println( calendar.getDisplayName(DAY_OF_WEEK, LONG, Locale.CHINA) );

}
}
44 changes: 44 additions & 0 deletions Week7/CalendarDate8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.time.*;
import java.time.format.*;

class CalendarDate8{
public static void main(String[] args) throws java.text.ParseException
{
//ʹ��Ĭ��ʱ��ʱ��˲ʱʱ�䴴�� Clock.systemDefaultZone() -->������� ZoneId.systemDefault()Ĭ��ʱ��
LocalDateTime now = LocalDateTime.now();
System.out.println(now);

//�Զ���ʱ��
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);//������Ӧ��ʱ����ʾ����

//����һ������
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59, 59);

//����String--->LocalDateTime
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59:59");
System.out.println(d4);

//ʹ��DateTimeFormatter API ���� �� ��ʽ��
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime d6 = LocalDateTime.parse("2013/12/31 23:59:59", formatter);
System.out.println(formatter.format(d6));

//ʱ���ȡ��һ����
System.out.println(d6.getYear());
System.out.println(d6.getMonth()); //�ⲻ������������ö��
System.out.println(d6.getDayOfYear());
System.out.println(d6.getDayOfMonth());
System.out.println(d6.getDayOfWeek());
System.out.println(d6.getHour());
System.out.println(d6.getMinute());
System.out.println(d6.getSecond());
System.out.println(d6.getNano()); //����

//ʱ������
LocalDateTime d7 = d6.minusDays(1);
LocalDateTime d8 = d6.plusHours(1).plusMinutes(30);
System.out.println(d7);
System.out.println(d8);
}
}
56 changes: 56 additions & 0 deletions Week7/CayleyTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.awt.*;
import javax.swing.*;

public class CayleyTree extends JFrame
{
public static void main( String []args ){
CayleyTree frm = new CayleyTree("CayleyTree");
frm.setVisible( true );
SwingUtilities.invokeLater(()->{
frm.init();
frm.drawTree( 10, 200, 400, 100, -Math.PI/2 );
});
}
private Frame frm;
private Graphics graphics;
private int width;
private int height;
private final double PI = Math.PI;
private final double th1 = 30 * PI / 180;
private final double th2 = 20 * PI / 180;
private final double per1 = 0.6;
private final double per2 = 0.7;

public CayleyTree(String title){
super(title);
setSize( 400, 440 );
setBackground( Color.lightGray );
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void init(){
width = getSize().width;
height = getSize().height;
graphics = this.getGraphics();
}



void drawTree(int n,
double x0, double y0, double leng, double th)
{
if( n==0 ) return;

double x1 = x0 + leng * Math.cos(th);
double y1 = y0 + leng * Math.sin(th);

drawLine(x0, y0, x1, y1);

drawTree( n - 1, x1, y1, per1 * leng, th + th1 );
drawTree( n - 1, x1, y1, per2 * leng, th - th2 );
}
void drawLine( double x0, double y0, double x1, double y1 ){
graphics.drawLine( (int)x0, (int)y0, (int)x1, (int)y1 );
}

}
12 changes: 12 additions & 0 deletions Week7/Fac.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Fac
{
public static void main(String args[])
{
System.out.println("Fac of 5 is " + fac( 5) );
}
static long fac( int n ){
if( n==0 ) return 1;
else return fac(n-1) * n;
}

}
23 changes: 23 additions & 0 deletions Week7/GenericMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;

class GenericMethod {
public static void main(String[] args){
Date date = BeanUtil.<Date>getInstance("java.util.Date");
System.out.println(date);
}
}

class BeanUtil{
public static <T> T getInstance( String clzName ){
try
{
Class c = Class.forName(clzName);
return (T) c.newInstance();
}
catch (ClassNotFoundException ex){}
catch (InstantiationException ex){}
catch (IllegalAccessException ex){}
return null;
}
}

34 changes: 34 additions & 0 deletions Week7/GenericTreeClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;

class GenericTreeClass {
public static void main(String[] args){
TNode<String> t = new TNode<>("Roo");
t.add("Left"); t.add("Middle"); t.add("Right");
t.getChild(0).add("aaa");
t.getChild(0).add("bbb");
t.traverse();
}
}

class TNode<T>
{
private T value;
private ArrayList<TNode<T>> children = new ArrayList<>();

TNode(T v) { this.value = v; }
public T getValue() { return this.value; }
public void add(T v) {
TNode<T> child = new TNode<>(v);
this.children.add(child);
}
public TNode<T> getChild(int i) {
if ((i < 0) || (i > this.children.size())) return null;
return (TNode<T>)this.children.get(i);
}

public void traverse() {
System.out.println(this.value);
for (TNode child : this.children)
child.traverse();
}
}
62 changes: 62 additions & 0 deletions Week7/Queen8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Queen8
{
public static void main(String[] args)
{
new Queen8().solve();
}

private static final int N = 8;
private int[]y; //��¼ÿ���ϵĻʺ�ŵ�λ��
int count = 0; //��ĸ���

public void solve()
{
count = 0;
y = new int[N+1]; //��ʼ������
int x = 1;

while(x>0)
{
y[x]++; //Ϊ��ǰxλ����һ���ʺ��λ��
while((y[x]<=N) && (!check(x))) y[x]++; //�ҵ����ʵĻʺ�
//
if(y[x]<=N)//�ҵ�һ�����Է��õ�x���ʺ��λ�ã����ò�Ϊֹ�����󲿷ֽⶼ����Ҫ��
{
if(x==N)//�ҵ�һ�������ķ��÷���
{
count++;
print();
}
else
x++; //����Ѱ����һ���ʺ��λ�ã���û�ҵ������������
}
else//δ�ҵ����Է��õ�x���ʺ��λ�ã����ò�Ϊֹ���Ѿ�֪��������Ҫ��
{
y[x] = 0;//��ΪҪ���ݣ���һ����Ѱ�ҵ�x-1���ʺ��λ�ã�
//����һ��ȷ��x-1��λ��֮�󣬵�x���ʺ�Ŀ�ʼ������λ��Ҫ����
x--; //����
}
}
}
private boolean check( int k) //���ԺϷ���
{
for(int j = 1;j < k;j++)
if((Math.abs(k-j) == Math.abs(y[j] - y[k]))
||(y[j] == y[k])) return false;
return true;
}

private void print()//��ʾ
{
System.out.println(count);
for( int i=1; i<=N; i ++ )
{
for( int j=1; j<=N; j++ )
if( j==y[i] ) System.out.print("x");//�����λ�÷��˻ʺ�����ʾx
else System.out.print("o");
System.out.println();
}

}

}
21 changes: 21 additions & 0 deletions Week7/StringAndStringBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.*;
class StringAndStringBuffer
{
public static void main(String[] args)
{
String a = "a";
String s = "";
StringBuffer sb = new StringBuffer();

final int N = 10000;

long t0 = System.currentTimeMillis();
for( int i=0; i<N; i++) s+=a;
long t1 = System.currentTimeMillis();
for( int i=0; i<N; i++) sb.append(a);
long t2 = System.currentTimeMillis();

System.out.println(t1-t0);
System.out.println(t2-t1);
}
}
13 changes: 13 additions & 0 deletions Week7/SystemProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.*;
class SystemProperties
{
public static void main(String[] args)
{
Properties props = System.getProperties();
Enumeration keys = props.propertyNames();
while(keys.hasMoreElements() ){
String key = (String) keys.nextElement();
System.out.println( key + " = " + props.getProperty(key) );
}
}
}
38 changes: 38 additions & 0 deletions Week7/TestArraysSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;

public class TestArraysSort {
static Random r = new Random();
static String
ssource = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static char[] src =
ssource.toCharArray();

static String randString(int length) {
char[] buf = new char[length];
for(int i = 0; i < length; i++) {
int rnd = Math.abs(r.nextInt()) % src.length;
buf[i] = src[rnd];
}
return new String(buf);
}

static String[] randStrings(int length, int size) {
String[] s = new String[size];
for(int i = 0; i < size; i++)
s[i] = randString(length);
return s;
}
public static void print(String[] s) {
for(int i = 0; i < s.length; i++)
System.out.print(s[i] + " ");
System.out.println();
}
public static void main(String[] args) {
String[] s = randStrings(4, 10);
print(s);
Arrays.<String>sort(s);
print(s);
int loc = Arrays.<String>binarySearch(s, s[2]);
System.out.println("Location of " + s[2] + " is " + loc);
}
}
51 changes: 51 additions & 0 deletions Week7/TestCollectionsSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.util.*;
class TestCollectionsSort
{
public static void main(String[] args)
{
List<Person> school = new ArrayList<>();
school.add( new Person("Li",23));
school.add( new Person("Wang",28));
school.add( new Person("Zhang",21));
school.add( new Person("Tang",19));
school.add( new Person("Chen",22));
school.add( new Person("Zhao",22));
System.out.println( school );

Collections.sort( school, new PersonComparator() );
System.out.println( school );

int index = Collections.binarySearch(
school, new Person("Li",23), new PersonComparator() );
if( index >=0 )
System.out.println( "Found:" + school.get( index ));
else
System.out.println( "Not Found!" );
}
}

class Person
{
String name;
int age;
public Person( String name, int age){
this.name=name;
this.age=age;
}
@Override
public String toString(){
return name+":"+age;
}
}

class PersonComparator implements Comparator
{
public int compare( Object obj1, Object obj2 ){
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
if( p1.age > p2.age ) return 1;
else if(p1.age<p2.age) return -1;
return p1.name.compareTo( p2.name );
}
}

Loading