标签:and tee ret cas class live 代码 说明 math
相关工具
Insert Live Template:插入一些记不起来的 Live Template 缩写
1.添加搭档仓库
2.产品代码
/**
* @author Jason Tong
* @date 2019/4/29 14:32.
*/
public class Complex {
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex(){}
public Complex(double R,double I){
ImagePart = I;
RealPart = R;
}
public boolean equals(Object obj){
if(this == obj) {
return true;
}
if(!(obj instanceof Complex)) {
return false;
}
Complex complex = (Complex) obj;
if(complex.RealPart != ((Complex) obj).RealPart) {
return false;
}
if(complex.ImagePart != ((Complex) obj).ImagePart) {
return false;
}
return true;
}
public String toString() {
String str = "";
if (ImagePart > 0)
str = RealPart + "+" + ImagePart + "i";
if (ImagePart == 0)
str = RealPart + "";
if (ImagePart < 0)
str = RealPart + " " + ImagePart + "i";
return str;
}
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a) {
return new Complex(RealPart+a.RealPart,ImagePart+a.ImagePart);
}
Complex ComplexSub(Complex a) {
return new Complex(RealPart-a.RealPart,ImagePart-a.ImagePart);
}
Complex ComplexMulti(Complex a) {
return new Complex(RealPart*a.RealPart,ImagePart*a.ImagePart);
}
Complex ComplexDiv(Complex a) {
if(a.RealPart==0||a.ImagePart==0) {
System.out.println("被减数不能为0");
return new Complex();
}
double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
return new Complex((RealPart*a.RealPart+ImagePart*a.ImagePart)/d,Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d));
}
}
3.测试代码
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
Complex complex = new Complex(1,1);
@Test
public void testAdd(){
assertEquals(new Complex(4.3,4.4), complex.ComplexAdd(new Complex(3.3,3.4)));
}
//测试加法
@Test
public void testSub(){
assertEquals(new Complex(-4.3,-3.4), complex.ComplexSub(new Complex(5.3,4.4)));
}
//测试减法
@Test
public void testMulti(){
assertEquals(new Complex(4.0,3.0), complex.ComplexMulti(new Complex(4.0,3.0)));
}
//测试乘法
@Test
public void testDiv(){
assertEquals(new Complex(1.0,1.0), complex.ComplexDiv(new Complex(1.0,1.0)));
assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(1.0,0.0)));
//assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(3,4)));
//边缘测试
}
@Test
public void testequals(){
assertEquals(true, complex.equals(new Complex(1.0,1.0)));
}
//测试判断相等
}
原代码为:
?
class A {
final double PI=3.1415926;// PI是常量
public double getArea(final double r) {
return PI*r*r;
}
public final void speak() {
System.out.println("您好,How's everything here ?");
}
}
public class Example5_9 {
public static void main(String args[]) {
A a=new A();
System.out.println("面积:"+a.getArea(100));
a.speak();
}
}
对类名以及变量名进行重构
进行封装
重构后的代码为:
/**
* @author Jason Tong
* @date 2019/4/29 16:53.
*/
class Calculate {
final double PI=3.1415926;// PI是常量
private int r;
public double getArea() {
return PI* getR() * getR();
}
public final void speak() {
System.out.println("您好,How's everything here ?");
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
public class Example5_9 {
public static void main(String args[]) {
Calculate a=new Calculate();
a.setR(10);
System.out.println("面积:"+a.getArea());
a.speak();
}
}
用java实现凯撒密码
代码为:
/**
* @author Jason Tong
* @date 2019/5/3 16:58.
*/
public class Caesar {
public static void main(String args[]) throws Exception{
String s=args[0];
int key=Integer.parseInt(args[1]);
Movement m=new Movement();
int n=s.length();
String es="";
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c >= 'a' && c <= 'z'){
es=m.realizeMove(n,c,key,'a','z');
}
else if (c >= 'A' && c <= 'Z'){
es=m.realizeMove(n,c,key,'A','Z');
}
}
System.out.println(es);
}
}
/**
* @author Jason Tong
* @date 2019/5/3 16:59.
*/
public class Movement {
String es="";
public String realizeMove(int n,char c,int key,char a,char b){
//移动key%26位
c+=key%26;
if(c<a) {
c+=26;
//向左超界
}
if(c>b) {
c-=26;
//向右超界
}
es+=c;
return es;
}
}
File>Setting>Editor>File and Code Template
按照下图输入开发者信息,新建类时自动添加信息。完成代码规范
解决办法二: 根据命令行提示,表明身份信息,即可推送
血泪教训!!! 修改博客随笔时一定要及时备份!!经常记得git pull
!!!
https://blog.csdn.net/weixin_42254058/article/details/81219931
http://www.cnblogs.com/rocedu/p/4795776.html
标签:and tee ret cas class live 代码 说明 math
原文地址:https://www.cnblogs.com/thz666/p/10805714.html