-1-
`import java.util.Scanner;
public class Test1 {
public static String prefix(String s1,String s2){
int i=0,j=-1;
for (i=0;i<s1.length();i++){
if (s1.charAt(i)==s2.charAt(i))
{
continue;
}
else{
break;
}
}
j=i;
if (j!=-1){
String str="";
for (i=0;i<j;i++){
str+=s1.charAt(i);
}
return str;
}
else
return "";
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
String s1,s2;
s1=sc.next();
s2=sc.next();
System.out.println(prefix(s1,s2));
}
}
-2-
import java.util.*;
public class Test2 {
public static String sort(String s){
char[] str = s.toCharArray();
Arrays.sort(str);
String t="";
for (int i=0;i<s.length();i++){
t+=str[i];
}
return t;
}
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
String s;
s = sc.next();
System.out.println(sort(s));
}
}
-3-
`import java.util.*;
public class Test3 {
public static boolean isNumberic2 (String token)
{
int i,flag=1;
for (i=1;i<token.length();i++){
if (token.charAt(0)!='.' ){
if ((token.charAt(i)>'0' && token.charAt(i)<'9') || token.charAt(i)=='.')
{
flag=1;
continue;
}
else {
flag=0;
break;
}
}
}
if (flag==0)
return false;
else
return true;
}
}
-4-
public class IllegalTriangleException extends Exception{
public IllegalTriangleException(){
super();
}
public IllegalTriangleException(String msg){
super(msg);
}
}
import java.util.*;
public class Trangle{
public static void Triangle1(double side1,double side2,double side3) throws IllegalTriangleException{
if (side1+side2<side3 || side1+side3<side2 || side3+side2<side1){
throw new IllegalTriangleException("创建错误");
}
else{
System.out.println("创建成功");
}
}
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
double side1,side2,side3;
side1 = sc.nextDouble();side2 = sc.nextDouble();side3 = sc.nextDouble();
try {
Triangle1(side1,side2,side3);
}
catch (IllegalTriangleException e){
e.printStackTrace();
}
}
}
-5-
import java.util.*;
public class Test5 {
public static void main (String[] args){
double [][] a = new double[3][3];
double [][] b = new double[3][3];
double [][] c = new double[3][3];
int i,j;
Scanner sc =new Scanner(System.in);
System.out.print("Enter matrix1: ");
for (i=0;i<3;i++){
for(j=0;j<3;j++){
a[i][j]=sc.nextDouble();
}
}
System.out.print("Enter matrix2: ");
for (i=0;i<3;i++){
for(j=0;j<3;j++){
b[i][j]=sc.nextDouble();
}
}
for (i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("The matrices are added as follows : ");
for (i=0;i<3;i++){
for(j=0;j<3;j++){
System.out.printf("%.1f ",a[i][j]);
}
if(i!=1){
System.out.print(" ");
for(j=0;j<3;j++){
System.out.printf("%.1f ",b[i][j]);
}
System.out.print(" ");
for(j=0;j<3;j++){
System.out.printf("%.1f ",c[i][j]);
}
}
else
{
System.out.printf(" + ");
for(j=0;j<3;j++){
System.out.printf("%.1f ",b[i][j]);
}
System.out.printf(" = ");
for(j=0;j<3;j++){
System.out.printf("%.1f ",c[i][j]);
}
}
System.out.println("");
}
}
}
-6-
import java.util.*;
public class Complex{
// protected Object clone() throws CloneNotSupportedException {
//
// return super.clone();
//
// }
double a,b;
@SuppressWarnings("unused")
private Complex(){
a=0;b=0;
}
public Complex(double a){
this.a=a;b=0;
}
public Complex(double a,double b){
this.a=a;this.b=b;
}
public double getRealPart()
{
return a;
}
public double getImaginaryPart()
{
return b;
}
public static void main(String[] args) throws CloneNotSupportedException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first complex number: ");
double a,b,c,d;
a = sc.nextDouble();b = sc.nextDouble();
final Complex com1 = new Complex(a,b);
System.out.print("Enter the secound complex number: ");
c = sc.nextDouble();d = sc.nextDouble();
final Complex com2 = new Complex(c,d);
class add {
double f=com1.a+com2.a;double g=com1.b+com2.b;
public String toString(){
return "("+com1.a+"+"+com1.b+"i ) "+"+ ("+com2.a+"+"+com2.b+"i ) = "+getRealPart()+"+"+getImaginaryPart()+"i";
}
public double getRealPart()
{
return f;
}
public double getImaginaryPart()
{
return g;
}
}
class subtract {
double f=com1.a-com2.a;double g=com1.b-com2.b;
public String toString(){
return "("+com1.a+"+"+com1.b+"i ) "+"- ("+com2.a+"+"+com2.b+"i ) = "+getRealPart()+"+"+getImaginaryPart()+"i";
}
public double getRealPart()
{
return f;
}
public double getImaginaryPart()
{
return g;
}
}
class multiply{
double f=com1.a*com2.a-com1.b*com2.b;double g=com1.b*com2.a+com1.a*com2.b;
public String toString(){
return "("+com1.a+"+"+com1.b+"i ) "+"* ("+com2.a+"+"+com2.b+"i ) = "+getRealPart()+"+"+getImaginaryPart()+"i";
}
public double getRealPart()
{
return f;
}
public double getImaginaryPart()
{
return g;
}
}
class divide{
double f=(com1.a*com2.a+com1.b*com2.b)/(com2.b*com2.b+com2.a*com2.a);double g=(com1.b*com2.a-com1.a*com2.b)/(com2.b*com2.b+com2.a*com2.a);
public String toString(){
return "("+com1.a+"+"+com1.b+"i ) "+"/ ("+com2.a+"+"+com2.b+"i ) = "+getRealPart()+"+"+getImaginaryPart()+"i";
}
public double getRealPart()
{
return f;
}
public double getImaginaryPart()
{
return g;
}
}
class abs{
double f=Math.pow(com1.a*com1.a+com1.b*com1.b, 0.5);
public String toString(){
return "|("+com1.a+"+"+com1.b+"i )| = "+answer();
}
public double answer()
{
return f;
}
}
add Add = new add();subtract Subtract = new subtract();
multiply Multiply = new multiply();divide Divide = new divide();
abs Abs = new abs();
System.out.println(Add.toString());
System.out.println(Subtract.toString());
System.out.println(Multiply.toString());
System.out.println(Divide.toString());
System.out.println(Abs.toString());
}
}