Advanced Programming-Week13
Summary
- 随机数 Random
- 自定义方法
Task64
package Week13;
import java.util.*;
public class Task64 {
public static void generateDirectionsToTake(String directions[],int cnt[])
{
Random rand = new Random();
int t=0;
System.out.println("The next 10 random directions to take are:");
for(int i=0;i<10;i++)
{
t=rand.nextInt(8);
cnt[t]++;
System.out.println(directions[t]);
}
System.out.println("\nThe frequency of each direction is as follows:");
for(int i=0;i<8;i++)
{
System.out.println(cnt[i]+"\t"+directions[i]);
}
}
public static void main(String []args)
{
Random rand = new Random();
String directions[]={"North", "South", "East", "West","North East","North West","South East","South West"};
int cnt[] = new int[10];
generateDirectionsToTake(directions,cnt);
}
}
Task67
package Week13;
import java.util.*;
public class Task67 {
private void input(int a[])
{
int cnt=0;
Scanner in = new Scanner(System.in);
int t;
while(cnt<25)
{
t=in.nextInt();
if(t!=0&&t!=1&&t!=3)
{
System.out.println("The number your enter is not right. Please enter a new number.");
continue;
}else{
a[cnt]=t;
cnt++;
}
}
}
private void output(int a[])
{
int cnt[]=new int[4];
for(int i=0;i<25;i++)
{
cnt[a[i]]++;
}
System.out.println("Outcome\tFrequency\t%");
System.out.println("Wins\t"+cnt[0]+"\t\t"+(int)((cnt[0]/25.0)*100));
System.out.println("Draws\t"+cnt[1]+"\t\t"+(int)((cnt[1]/25.0)*100));
System.out.println("Losses\t"+cnt[3]+"\t\t"+(int)((cnt[3]/25.0)*100));
}
private void cal(int a[])
{
for(int i=0;i<24;i++)
{
for(int j=i+1;j<25;j++)
{
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
public static void main(String []args)
{
Task67 obj= new Task67();
int a[]= new int[30];
for(int i=0;i<25;i++)
{
a[i]=i;
}
obj.input(a);
obj.cal(a);
obj.output(a);
}
}
Task68
package Week13;
import java.util.*;
public class Task68 {
private void input(int a[])
{
int cnt=0;
Scanner in = new Scanner(System.in);
Random r = new Random();
for(int i=0;i<50;i++)
{
a[i]=r.nextInt(1000)+1;
}
}
private void output(int a[],int cnt[])
{
for(int i=0;i<10;i++)
{
if(cnt[i]==0)
continue;
System.out.println(cnt[i]+" bumber(s) end with "+i);
}
}
private void cal(int a[],int cnt[])
{
for(int i=0;i<50;i++)
{
cnt[a[i]%10]++;
}
public static void main(String []args)
{
Task68 obj= new Task68();
int a[]= new int[100];
int cnt[]= new int[11];
for(int i=0;i<25;i++)
{
a[i]=i;
}
obj.input(a);
obj.cal(a,cnt);
obj.output(a,cnt);
}
}
Task70
package Week13;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
public class Task70 {
public static void calculateProjectCost(String args[])
{
double ans=0;
switch (Integer.parseInt(args[0]))
{
case 1:
switch (Integer.parseInt(args[1]))
{
case 1: ans=5600.50;break;
case 2: ans=10600.50;break;
case 3: ans=17000.00;break;
}
case 2:
switch (Integer.parseInt(args[1]))
{
case 1: ans=5500.00;break;
case 2: ans=11000.00;break;
case 3: ans=22000.00;break;
}
case 3:
switch (Integer.parseInt(args[1]))
{
case 1: ans=6000.00;break;
case 2: ans=13500.00;break;
case 3: ans=25000.00;break;
}
}
System.out.println("The cost is "+ans);
}
public static void main(String[] args)
{
if(args.length==0)
{
System.out.println("The input is empty!");
}else{
if(args.length!=2)
{
System.out.println("The number of your input is not right");
}else{
if(Integer.parseInt(args[0])<1||Integer.parseInt(args[0])>3||Integer.parseInt(args[1])<1||Integer.parseInt(args[1])>3)
{
System.out.println("The range of your input is not right");
}else{
calculateProjectCost(args);
}
}
}
}
}
Task71
package Week13;
public class Task71 {
public static void main(String[] args)
{
int day,month,year;
day=Integer.parseInt(args[0]);
month=Integer.parseInt(args[1]);
year=Integer.parseInt(args[2]);
int monthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month!=2)
{
if(day==monthDay[month])
{
System.out.println("The "+year+"-"+month+"-"+day+" is a right date.");
}else{
System.out.println("The date is not right.");
}
}else{
if(year%400==0||(year%4==0&&year%100!=0))
{
if(day==29)
System.out.println("The "+year+"-"+month+"-"+day+" is a right date.");
else
System.out.println("The date is not right.");
}else{
if(day==28)
System.out.println("The "+year+"-"+month+"-"+day+" is a right date.");
else
System.out.println("The date is not right.");
}
}
}
}
Task72
package Week13;
import java.util.*;
public class Task72 {
public static void main(String[] args)
{
int t=0;
int cnt[]= new int[15];
Scanner in=new Scanner(System.in);
int tot=0;
while(true)
{
t=in.nextInt();
if(t!=-1)
{
tot++;
cnt[t]++;
}else{
break;
}
}
int sum=0;
for(int i=1;i<=10;i++)
{
sum+=cnt[i]*i;
}
System.out.println("The count of number:");
for(int i=1;i<=10;i++)
{
System.out.println("The count of "+i+" is "+cnt[i]);
}
System.out.println("The frequency of number:");
for(int i=1;i<=10;i++)
{
System.out.println("The frequency of "+i+" is "+((double)cnt[i]/sum));
}
System.out.println("The average of number is "+((double)sum/tot));
}
}
Task73
package Week13;
public class Task73 {
public static void main(String[] args)
{
if(args[3]=="2")
{
String t=args[0];
args[0]=args[1];
args[1]=t;
}
int rows=Integer.parseInt(args[0]);
int columns=Integer.parseInt(args[1]);
for(int i=rows; i>0; i--)
{
for(int j=columns; j>0; j--)
{
if(i%2==j%2)
{
System.out.print(args[2]);
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Task 74
package Week13;
import java.util.*;
public class Task74 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s=in.nextLine();
int t=0;
for(int i=s.length()-1; i>=0; i--)
{
t=s.charAt(i);
int tt=0;
while(t!=0)
{
tt=t%10;
t/=10;
System.out.print(tt);
}
System.out.print(".");
}
}
}
Task 75
Task76
package Week13;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Task76 {
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
String regex="^[^aeiou]";
String string=in.nextLine();
Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(string);
if(matcher.find()!=true)
{
string=string +"way";
}else{
string = string.substring(1);
string = string +matcher.group()+"ay";
}
System.out.println(string);
}
}
Task 77
package Week13;
import java.util.*;
import java.util.regex.*;
public class Task77 {
public static void main(String []args)
{
String w1;
Scanner in = new Scanner(System.in);
w1=in.nextLine();
String regex="a[a-z]*b";
Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(w1);
if(matcher.find()==true)
{
System.out.println("The word "+w1+" is a right word.");
}else{
System.out.println("The word "+w1+" is not a right word.");
}
}
}
Task78
package Week13;
import java.util.*;
import java.util.regex.*;
public class Task78 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String regex="[aeiou]y$";
String s1=in.nextLine();
Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(s1);
if (matcher.find())
{
s1=s1+"s";
}else{
s1=s1.substring(0,s1.length()-1)+"ies";
}
System.out.println(s1);
}
}
Task 79
package Week13;
import java.util.*;
import java.util.regex.*;
public class Task79{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
String regex="([(aeiou)])";
String s1=in.nextLine();
Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(s1);
int cnt=0,tot=s1.length();
while(matcher.find())
{
cnt++;
}
System.out.println("There are "+cnt+" vowels and "+(tot-cnt)+" consonants in the word "+s1+".");
}
}
Task 80
package Week13;
import java.util.*;
import java.util.regex.*;
public class Task80 {
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
String regex="([a-z]*)";
String s1=in.nextLine();
Pattern pattern= Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher matcher= pattern.matcher(s1);
int cnt=0;
while(matcher.find())
cnt++;
int cost=cnt*5;
System.out.println("The cost is "+cost+".");
}
}
Task 82
package Week13;
import javax.swing.*;
import java.util.*;
import java.util.regex.*;
public class Task82 {
public static void main(String[]args)
{
try{
String input=JOptionPane.showInputDialog(null,"Pangram Checker:\nPlease enter sentence to check.");
int tot[]= new int[26];
int sum=0;
for(int i=0;i<input.length();i++)
{
if(tot[input.charAt(i)-'a']==0)
{
tot[input.charAt(i)-'a']=1;
sum++;
}
}
String result="";
if(sum==26) result="The quick brown fox jumos over a lazy dog.\nis a pangram";
else result="The quick brown fox jumos over a lazy dog.\nis not a pangram";
JOptionPane.showMessageDialog(null,result);
}catch (NullPointerException Error){
JOptionPane.showMessageDialog(null,"No input provided");
}
}
}
Task 83
package Week13;
import java.util.*;
import java.util.regex.*;
public class Task83 {
public static void main(String []args)
{
String password="";
Scanner in= new Scanner(System.in);
password = in.nextLine();
String regex[]={"[\s]","[A-Z]","[a-z]","[0-9]","[^d0-9a-zA-Z\s]"};
Pattern pattern=Pattern.compile(regex[0]);
Matcher matcher=pattern.matcher(password);
String result="The password is right.";
if(password.length()<6&&password.length()>10)
{
result="Sorry, the password is not right.";
}
if(matcher.find())
{
result="Sorry, the password is not right.";
}else{
for(int i=1;i<5;i++)
{
pattern=Pattern.compile(regex[i]);
matcher=pattern.matcher(password);
if(matcher.find())
{
continue;
}else{
result="Sorry, the password is not right.";
}
}
}
System.out.println(result);
}
}