id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
8739008f-05eb-4e9b-911c-e4183f491cac
|
public static void main(String[] args) {
receive(createNewIssueCase);
receive(createLink);
receive(addressNewLink);
//gets the name of the current method
/*String name = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("*** name: ***" + name);*/
}
|
4a7a3e7d-0105-45c5-a938-5c59e661e466
|
public static void receive(String[] locatorInfo){
locators locator = null;
locator = locators.valueOf(locatorInfo[1]);
System.out.println("*** before displaying info in receive method***");
switch(locator){
case xpath:
System.out.println("inside locator: " + locator);
break;
case classname:
System.out.println("inside locator: " + locator);
break;
case link:
System.out.println("inside locator: " + locator);
break;
case id:
System.out.println("inside locator: " + locator);
break;
default:
System.out.println("*** NO locator defined for: " + locator);
break;
}
System.out.println("*** exiting receive *** \n");
}
|
9910b961-3e2a-40f4-bb33-47fe7f5cc64a
|
public static void main(String[] args) {
JFrame main = new JFrame("First Frame"); //Creating the new frame
main.setVisible(true); //Making the frame visible
main.setTitle("My First frame"); //Setting a title for this frame
main.setSize(800, 300); //Setting the size to this frame
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //what to do on close
main.setLocationRelativeTo(null); //Always show centered on first opening
System.out.println("hello world!");
System.out.println("hello world 2");
}
|
40a8cbde-81f5-47be-bb0c-84fc54534df0
|
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.println("Ping goes here... " + i);
// isReachableByPing("www.google.com");
try {
Object redexterna = InetAddress.getByAddress("www.google.com".getBytes()).isReachable(1000);
System.out.println("Value is" + redexterna);
Thread.sleep(3000);
} catch (UnknownHostException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
d4521d10-87ce-4737-acb0-f08ace1204d0
|
public static boolean isReachableByPing(String host) {
try {
String cmd = "";
if (System.getProperty("os.name").startsWith("Windows")) {
// For Windows
cmd = "ping -n 1 " + host;
} else {
// For Linux and OSX
cmd = "ping -c 1 " + host;
}
System.out.println("cmd: " + cmd);
Process myProcess = Runtime.getRuntime().exec(cmd);
System.out.println("my process: " + myProcess.waitFor());
myProcess.waitFor();
if (myProcess.exitValue() == 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
|
2c8c3200-b798-46eb-911d-6569533c19d6
|
public static void main(String[] args) {
File file = new File("./");
String[] arrayStr = new String[MAX];
if (file.isDirectory()) {
arrayStr = file.list();
}
else {
System.err.printf("%s is not directory", file.getName());
System.exit(1);
}
for (String str : arrayStr) {
System.out.println(str);
}
}
|
2525ac7a-cfed-4078-b702-d3fffefa0db0
|
public static void main(String[] args)
throws IOException {
String fileName = PRE + random.nextInt(100);
File file = new File("./", fileName);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(STR.getBytes());
bos.close();
fos.close();
}
|
8678661e-4bab-43e0-a04f-b12f81b89077
|
public static void main(String[] args) {
File file = new File("./");
System.out.println(file.getAbsolutePath());
}
|
da683e9f-cdac-41fc-beca-767c75a063cb
|
public static void main(String[] args)
throws FileNotFoundException {
boolean isCreate = false;
String dirName = PRE + random.nextInt(100);
File file = new File("./", dirName);
if (file.exists()) {
System.err.printf("%s exists!\n", dirName);
System.exit(1);
}
else {
isCreate = file.mkdir();
}
if (isCreate) {
System.out.printf("created %s", file.getAbsolutePath());
}
else {
System.out.println("fail to create");
}
}
|
93c9057d-97a0-4eb4-8c1e-b963a9905432
|
public static void main(String[] args)
throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Directory: ");
String dir = scanner.nextLine();
System.out.print("Filename: ");
String file = scanner.nextLine();
File target = new File(dir + file);
if (target.exists())
System.out.println(target.getAbsolutePath());
else
System.out.println("File not exist");
}
|
af7ebd32-9c21-4126-afe1-c47c72f22b4b
|
public static void main(String[] args) throws InterruptedException {
GetPi getPi = new GetPi();
Thread[] threads = new Thread[TOTAL_THREAD];
System.out.println("Number Of Spots : " + TOTAL_SPOT);
System.out.println("Number Of Threads: " + TOTAL_THREAD);
for (int idx = 0 ; idx < TOTAL_THREAD ; idx++) {
threads[idx] = new Thread(getPi);
threads[idx].start();
}
for (Thread thread : threads) {
thread.join();
}
System.out.println(pi());
}
|
8389355a-679f-43a6-bc84-59c1e019fea8
|
static double pi() {
return (IN_SPOT / TOTAL_SPOT * 4);
}
|
ca11b5b1-c37d-4ab1-9cda-fab7cf0e083b
|
public void run() {
int localSpot = 0;
Random random = new Random();
float x = 0;
float y = 0;
for (int idx = 0 ; idx < TOTAL_SPOT / TOTAL_THREAD ; idx++) {
x = random.nextFloat();
y = random.nextFloat();
if (x*x + y*y <= 1)
localSpot++;
}
synchronized(this) {
IN_SPOT = IN_SPOT + localSpot;
}
}
|
a090ed9f-8941-4294-bd08-ab1110faf1b0
|
public static void main(String[] args)
throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Source File Dir: ");
String input = scanner.nextLine();
System.out.print("Target File Dir: ");
String output = scanner.nextLine();
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output);
int data = 0;
do {
data = fis.read();
fos.write(data);
} while (data != -1);
fis.close();
fos.close();
}
|
7363776c-35f4-40b1-81f2-a118b2546be4
|
public void isALeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "年是闰年");
} else {
System.out.println(year + "年不是闰年");
}
}
|
77aca5ba-00b7-4fa5-844f-470cee90164e
|
public void checkLogin(String username, String password) {
/**
* 多个分支语句练习
*/
if (!username.equals("sun")) {
System.err.println("用户名输入错误!");
} else if (!password.equals("sun")) {
System.err.println("密码错误!");
} else {
System.out.println("欢迎您" + username + "先生");
}
}
|
99607a34-79d9-44ac-8843-c54334968045
|
public void deptDivide(String usrname, String language) {
String tmpStr = language.toLowerCase();
switch (tmpStr.hashCode()) {
case 3254818:
System.out.println(usrname + "被分到Java开发部门");
break;
case 96515:
System.out.println(usrname + "您好,您被分到C++开发部门");
break;
case 3104:
System.out.println(usrname + "您好,您被分到C#开发部门");
break;
default:
System.out.println(usrname + "您好,我们公司不需要" + language + "语言的开发人员");
}
}
|
4f27299c-bece-43c7-b6af-11d4b4283db3
|
public void ergodicArray(String[] arr) {
int index = 0;
String tmpStr = "一年共有";
while (index < arr.length) {
tmpStr += arr[index]+",";
index++;
}
tmpStr += arr.length+"个季节。";
System.out.println(tmpStr);
}
|
355e9205-cc9f-457e-8cfd-22bc16f9f0dd
|
public void valuePass(String str){
/* System.out.println("请输入一个数,i:");
int i = scanner.nextInt();
System.out.println("您输入的数为,i:"+i);
hello.valuePass(i);
System.out.println("您输入的i经过方法调用后,i = "+i);
System.out.println("传入i,i = "+i);
i++;
System.out.println("i自增后,i = "+i);
*/
System.out.println("传入str,str = "+str);
str += "您好!";
System.out.println("i自增后,str = "+str);
}
|
e7380c16-fd30-44dc-90b3-4e0ff589d1bd
|
public void refPass(Integer i){
System.out.println("您传入"+i);
i++;
System.out.println("改变您传入的东西"+i);
}
|
914060a1-16d4-45b3-a3fc-d3511ff38138
|
public void finalRelatedTest(){
final StringBuffer stringBuffer = new StringBuffer("sun");
System.out.println("final 引用不能变,其值能变,stringBuffer为"+stringBuffer+"\r\n"+"append(\"sean\")后");
stringBuffer.append("sean");
System.out.println(stringBuffer+"当你试图改变这个引用时");
}
|
a63c7c8c-1ce9-4d03-b363-ec34c1f145de
|
public void swap1(int m,int n){
int tmp = 0;
tmp = m;
m = n;
n = tmp;
System.out.println("i = "+m+" j = "+n);
}
|
a129bbdb-cb9b-4902-ae81-17372d370917
|
public void swap2(int m,int n){
m = m + n;
n = m - n;
m = m - n;
System.out.println("i = "+m+" j = "+n);
}
|
ddb27a4d-c459-411e-8b34-db525b485d82
|
public void swap3(int m,int n){
m = m ^ n;
n = m ^ n;
m = m ^ n;
System.out.println("i = "+m+" j = "+n);
}
|
5a6944a7-0738-4b23-a3d9-0311ea2c5552
|
public static void main(String[] args) {
Swap swap = new Swap();
int i = 1;
int j = 2;
System.out.println("i = 1 \r\nj = 2");
swap.swap1(i,j);
System.out.println("swap1");
swap.swap2(i,j);
System.out.println("swap2");
swap.swap3(i,j);
System.out.println("swap3");
}
|
3313d364-e5c8-499e-b2f0-8f9dbbd7ddcd
|
public static void main(String[] args) {
try {
PrintStream out = System.out; //保存原输出流
PrintStream ps = ps = new PrintStream("D:\\Download\\300Demos\\Day4\\src\\log.txt");//新建输出流
System.setOut(ps);
int age = 10;
System.out.println("定义年龄变量,初值为10");
String sex = "女";
System.out.println("定义性别变量,初值为女");
String info = "这个"+sex+"人"+age+"岁了。";
System.out.println("info整合使用sex和age变量:"+info);
System.setOut(out);
System.out.println("改变输出流实验结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
|
5b9a4873-2f92-45d7-8800-1c98acd77443
|
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Hello hello = new Hello();
hello.finalRelatedTest();
}
|
133b7543-1adc-41ee-8f4c-af494978c6f6
|
public static void main(String[] args) {
System.out.println("password:");//提示输入
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
System.out.println("输入密码为:"+password);
char[] arrray = password.toCharArray();
for (int i = 0; i < arrray.length;i++){
arrray[i] = (char)(arrray[i]^5656);
}
String encryptPwd = arrray.toString();
System.out.println("加密后"+encryptPwd);
}
|
347d54c6-ded3-486f-9043-7876eed80eb5
|
public OutPutDemo(Resource r) {
this.r = r;
}
|
581e7324-b5ea-4dfa-9e48-e570267c34fa
|
@Override
public void run() {
while (true) {
r.out();
}
}
|
12bb42bb-a5bd-45b9-b099-efbf129d54d5
|
public synchronized void set(String username,String sex){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.username = username;
this.sex = sex;
flag = true;
this.notify();
}
|
73b11556-3d9d-4b91-8f12-17a784e6f24a
|
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("姓名:" +username+ "..." + "性别:" +sex);
flag = false;
this.notify();
}
|
75edb922-23bd-446b-9c73-1692784cc000
|
public InputDemo(Resource r) {
this.r = r;
}
|
dc6d49eb-b319-4c72-acce-cff9721a8b2f
|
@Override
public void run() {
int x = 0;
while (true) {
if (x == 0) {
r.set("sun","male");
} else {
r.set("孙","男");
}
x = (x + 1) % 2;
}
}
|
96d9bbf2-9916-400f-b540-78459c9130c7
|
public static void main(String[] args) {
Resource r = new Resource();
InputDemo in = new InputDemo(r);
OutPutDemo ou = new OutPutDemo(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(ou);
t1.start();
t2.start();
}
|
a779dddf-8c8b-4c10-8f4a-3ff164cf5689
|
public static void main(String[] args) {
int[][] triangle = new int[10][]; //杨辉三角存入一个二维数组
for (int i = 0; i < triangle.length; i++) {//遍历第一层
triangle[i] = new int[i + 1]; //初始化第二层
for (int j = 0; j <= i; j++) {
if (i == 0 || j == 0 || j == i) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
System.out.print(triangle[i][j] + "\t");
}
System.out.println();
}
}
|
54f4c7f5-39f1-4866-a4a1-501bcc840508
|
public CalculatorFrame() {
super("Calculator");
pnlUpper.setLayout(new GridLayout(0, 3));
pnlUpper.add(new JLabel("f(x)"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtExpression);
pnlUpper.add(new JLabel("X"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtX);
txtY.setEditable(false);
pnlUpper.add(new JLabel("Y"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtY);
cmbTrig.addItem("Degree");
cmbTrig.addItem("Radian");
cmbTrig.addActionListener(this);
pnlLower.add(cmbTrig);
btnCalculate.addActionListener(this);
pnlLower.add(btnCalculate);
add(pnlUpper, BorderLayout.NORTH);
add(pnlLower, BorderLayout.SOUTH);
setSize(220, 160);
}
|
9703c196-376c-4f8e-96e5-505d01c3aa84
|
@Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btnCalculate) {
String exp = this.txtExpression.getText();
String x = this.txtX.getText();
if (!exp.isEmpty() && !x.isEmpty()) {
String result;
if(!exp.equals(prevExpression)){
parser.setExpression(exp);
prevExpression = exp;
}
try{
result = new DecimalFormat("#0.00").format(parser.eval(Double.parseDouble(x)));
this.txtY.setText(result);
} catch(IllegalArgumentException e){
this.txtY.setText("Undefined");
}
}
} else {
if (this.cmbTrig.getSelectedIndex() == 0)
this.parser.setIsRadian(false);
if (this.cmbTrig.getSelectedIndex() == 1)
this.parser.setIsRadian(true);
}
}
|
581ca48f-8996-4f42-a8c0-a31f61b9fd3a
|
public PlottingFrame(int width, int height) {
LayoutManager layout = new OverlayLayout();
setLayout(layout);
setSize(width, height);
this.xTicksNum = 11;
this.yTicksNum = 11;
this.expression = null;
this.interval = new Interval();
this.origin = new Point2D.Double(0, 0);
this.originLocation = new Point2D.Double(55, height);
this.lines = new ArrayList<Line2D.Double>();
this.points = new ArrayList<Point2D.Double>();
this.pointsLocations = new ArrayList<Point2D.Double>();
this.axisX = new Axis(new Point2D.Double(55, height), width - 10,
height - 30, 1);
this.axisY = new Axis(new Point2D.Double(55, 30), height - 30,
width - 10, -1);
}
|
a64bba13-1ae1-44b0-84de-c6b797ac84e3
|
public void setNumTicksX(int num) {
this.xTicksNum = num;
this.axisX.setNumTicks(num);
clearPoints();
}
|
05b9f960-b0d6-46f8-9f1c-035e60522d6e
|
public void setNumTicksY(int num) {
this.yTicksNum = num;
this.axisY.setNumTicks(num);
}
|
ebb8d646-d5f5-4273-9361-1258d57350fd
|
public void setExpression(String exp) {
this.expression = exp;
clearPoints();
}
|
a9e9bfc1-a295-4c2c-a2bd-7ed812667c18
|
public void setInterval(double start, double end) {
this.interval = new Interval(start, end);
clearPoints();
}
|
c1d32918-f681-4154-b527-4f1b8a0ac83a
|
public void setIsRadian(boolean b){
this.parser.setIsRadian(b);
}
|
9c0e2280-8d09-4253-a7ed-e724a4922270
|
public void repaintXAxis(){
this.axisX.repaint();
}
|
6476fb32-2477-4655-b8e4-58e7effd7008
|
public void repaintYAxis(){
this.axisY.repaint();
}
|
bf3e4422-b335-47bd-8985-5e86286de958
|
private void clearPoints(){
this.lines.clear();
this.points.clear();
this.pointsLocations.clear();
}
|
2e7f94f1-2f2d-4304-89b6-15dad4d1be08
|
private void generatePoints() {
if (this.expression != null) {
this.parser.setExpression(this.expression);
double steps = (this.interval.getDistance() + 1) / this.xTicksNum;
ArrayList<java.lang.Double> result = this.parser.eval(this.interval, steps);
double maxY = result.get(0);
double minY = result.get(0);
for (int i = 0; i < result.size(); i++) {
this.points.add(new Point2D.Double(this.interval.start + i
* steps, result.get(i)));
if (result.get(i) > maxY)
maxY = result.get(i);
if (result.get(i) < minY)
minY = result.get(i);
}
this.origin = new Point2D.Double(this.interval.start, minY);
this.axisX.setInterval(this.interval.start, this.interval.end);
this.axisX.setNumTicks(xTicksNum);
this.axisY.setInterval(minY, maxY);
this.axisY.setNumTicks(yTicksNum);
}
}
|
f8fc08a6-1c64-43c3-9909-c457e7379ec8
|
private void generatePointsLocations() {
if (!this.points.isEmpty()) {
double locX;
double locY;
double unitDistX = axisX.getUnitDistance();
double unitDistY = axisY.getUnitDistance();
double unitLengthX = axisX.getUnitLength();
double unitLengthY = axisY.getUnitLength();
for (int i = 0; i < this.points.size(); i++) {
locX = originLocation.x + (points.get(i).x - origin.x)
/ unitDistX * unitLengthX;
locY = originLocation.y - (points.get(i).y - origin.y)
/ unitDistY * unitLengthY;
pointsLocations.add(new Point2D.Double(locX, locY));
}
}
}
|
905b3f6b-baba-4954-b1ac-9053267a1087
|
private void generateLines() {
if (!pointsLocations.isEmpty()) {
for (int i = 0; i < pointsLocations.size(); i++) {
if (i + 1 != pointsLocations.size())
lines.add(new Line2D.Double(pointsLocations.get(i),
pointsLocations.get(i + 1)));
}
}
}
|
c915cb3c-5ac7-47e4-9be5-d4a95ee467ac
|
public void reset() {
this.expression = null;
this.axisX.setInterval(0, 10);
this.axisY.setInterval(0, 10);
this.axisX.setNumTicks(11);
this.axisY.setNumTicks(11);
clearPoints();
revalidate();
repaint();
}
|
68e7fda9-29e0-43f6-890f-39cddc3a0f51
|
public void plot() {
generatePoints();
generatePointsLocations();
generateLines();
add(axisX);
add(axisY);
revalidate();
repaint();
}
|
e44acde7-81d9-4648-a079-5a60264b5342
|
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D) g;
g2.setColor(RECT_COLOR);
g2.fillRect(55, 30, (int) axisX.getAxisLength(),
(int) axisY.getAxisLength());
g2.setColor(LINE_COLOR);
for (int i = 0; i < lines.size(); i++) {
g2.draw(lines.get(i));
}
}
|
cd639d75-a185-4328-ba67-4e18e2cd7e4e
|
public FunctionPlotter(int width, int height) {
super("Function Plotter");
setSize(width, height);
setIconImage(imiSmall.getImage());
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pfFrame.setIconImage(imiSmall.getImage());
pfFrame.setVisible(false);
cFrame.setIconImage(imiSmall.getImage());
cFrame.setVisible(false);
siFrame.setIconImage(imiSmall.getImage());
siFrame.setVisible(false);
sldX.addChangeListener(this);
sldX.setMajorTickSpacing(10);
sldX.setMinorTickSpacing(1);
sldX.setPaintTicks(true);
sldX.setPaintLabels(true);
sldY.addChangeListener(this);
sldY.setMajorTickSpacing(10);
sldY.setMinorTickSpacing(1);
sldY.setPaintTicks(true);
sldY.setPaintLabels(true);
mniPlotFunc.addActionListener(this);
mniCalculator.addActionListener(this);
mniExit.addActionListener(this);
mnuActions.add(mniPlotFunc);
mnuActions.add(mniCalculator);
mnuActions.add(mniExit);
mnb.add(mnuActions);
mniClear.addActionListener(this);
mniSetInterval.addActionListener(this);
mnuEdit.add(mniClear);
mnuEdit.add(mniSetInterval);
mnb.add(mnuEdit);
mniAbout.addActionListener(this);
mnuAbout.add(mniAbout);
mnb.add(mnuAbout);
pFrame = new PlottingFrame(width - 150, height - 150);
pFrame.plot();
add(mnb, BorderLayout.NORTH);
add(sldX, BorderLayout.SOUTH);
add(sldY, BorderLayout.EAST);
add(pFrame);
validate();
}
|
a81b6e1c-1ccb-4bb4-9e19-90eb4c5ac346
|
@Override
public void actionPerformed(ActionEvent a) {
Object source = a.getSource();
if (source == mniPlotFunc) {
pfFrame.setLocation(getWidth() / 2 + getLocation().x - 140,
getHeight() / 2 + getLocation().y - 100);
pfFrame.clearEntry();
pfFrame.pFrame = this.pFrame;
pfFrame.setVisible(true);
} else if (source == mniCalculator) {
cFrame.setLocation(
getWidth() / 2 + getLocation().x - 110, getHeight() / 2
+ getLocation().y - 80);
cFrame.setVisible(true);
}
if (source == mniExit) {
System.exit(0);
} else if (source == mniClear) {
pFrame.reset();
} else if (source == mniSetInterval) {
siFrame.setLocation(getWidth() / 2 + getLocation().x - 110,
getHeight() / 2 + getLocation().y - 90);
siFrame.clearEntry();
siFrame.pFrame = this.pFrame;
siFrame.setVisible(true);
} else if (source == mniAbout) {
JOptionPane.showMessageDialog(this, ABOUT, "About",
JOptionPane.INFORMATION_MESSAGE, imiBig);
}
}
|
10cbf927-8548-406a-aa42-c14c4cc27c05
|
@Override
public void stateChanged(ChangeEvent a) {
Object source = a.getSource();
if (source == sldX) {
pFrame.setNumTicksX(sldX.getValue());
pFrame.plot();
} else {
pFrame.setNumTicksY(sldY.getValue());
pFrame.repaintYAxis();
}
}
|
445fc951-cfb3-464c-9e4a-d368c91ccc94
|
public static void main(String[] args) {
new FunctionPlotter(1200, 700).setLocation(80, 20);
}
|
67598836-aca6-45dc-9b0f-c4792925656e
|
public SetIntervalFrame() {
super("Set Interval");
pnlUpper.setLayout(new GridLayout(0, 3));
pnlUpper.add(new JLabel("Start"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtStart);
pnlUpper.add(new JLabel("End"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtEnd);
pnlUpper.add(new JLabel("X Ticks"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtXTicks);
pnlUpper.add(new JLabel("Y Ticks"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtYTicks);
btnSet.addActionListener(this);
pnlLower.add(btnSet);
add(pnlUpper, BorderLayout.NORTH);
add(pnlLower, BorderLayout.SOUTH);
setSize(220, 180);
}
|
15a97e1e-8ee8-45ce-8f82-8adbfb030662
|
public void clearEntry(){
this.txtStart.setText(null);
this.txtEnd.setText(null);
this.txtXTicks.setText(null);
this.txtYTicks.setText(null);
}
|
fa1b1183-17f6-4080-9e25-c0d93d9f631d
|
@Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btnSet) {
String start = this.txtStart.getText();
String end = this.txtEnd.getText();
String xTicks = this.txtXTicks.getText();
String yTicks = this.txtYTicks.getText();
if (!start.isEmpty() && !end.isEmpty()) {
this.pFrame.setInterval(Double.parseDouble(start),
Double.parseDouble(end));
if(!xTicks.isEmpty())
this.pFrame.setNumTicksX(Integer.parseInt(xTicks));
if(!yTicks.isEmpty())
this.pFrame.setNumTicksY(Integer.parseInt(yTicks));
}
this.pFrame.plot();
this.setVisible(false);
}
}
|
ea2831ce-47a7-4900-a76e-bf34209e4c38
|
public Interval(){
this.start = 0;
this.end = 10;
}
|
17546714-118c-43c2-bd7b-52a5505603a3
|
public Interval(double start, double end){
this.start = start;
this.end = end;
}
|
6310bf4a-9daa-4a09-8c19-cde5a8a0a702
|
public double getDistance(){
return this.end - this.start;
}
|
18d6d79a-8afe-48b0-a386-9b6979451611
|
public Axis(Point2D.Double startPoint, double axisLength,
double dashLength, int orientation) {
this.ticksNum = 11;
this.interval = new Interval();
this.ticks = new Tick[11];
this.startPoint = startPoint;
this.axisLength = axisLength;
this.dashLength = dashLength;
this.unitDistance = 1;
this.unitLength = axisLength / 10;
this.orientation = Orientation.getOrien(orientation);
generateAxis();
}
|
ed22fb9b-b1ce-4b57-9ec8-89a2696fded7
|
public Axis(Point2D.Double startPoint, double axisLength,
double dashLength, Interval interval, int ticksNum,
int orientation) {
this.ticksNum = ticksNum;
this.interval = interval;
this.startPoint = startPoint;
this.axisLength = axisLength;
this.dashLength = dashLength;
this.ticks = new Tick[ticksNum];
this.unitLength = axisLength / (ticksNum - 1);
this.unitDistance = interval.getDistance() / (ticksNum - 1);
this.orientation = Orientation.getOrien(orientation);
generateAxis();
}
|
cd05e2f1-b149-4bf2-a071-a2583afba61d
|
public int getNumTicks() {
return this.ticksNum;
}
|
edb9ff60-7698-482c-86e2-586e3e657094
|
public double getAxisLength() {
return this.axisLength;
}
|
dfab4410-1f41-4748-9e64-76769588714f
|
public double getDashLength() {
return this.dashLength;
}
|
8f05c0bd-3587-4716-a4e6-0a7cbb86e107
|
public double getUnitLength() {
return this.unitLength;
}
|
ba1a1303-376a-44ab-8687-29695667d977
|
public double getUnitDistance() {
return this.unitDistance;
}
|
98c958dd-3812-4039-9925-3045fb949e20
|
public Interval getInterval() {
return this.interval;
}
|
9c5da47b-791e-4610-b606-04f08051828e
|
public void setNumTicks(int num) {
this.ticksNum = num;
this.ticks = new Tick[num];
this.unitLength = axisLength / (num - 1);
this.unitDistance = this.interval.getDistance() / (num - 1);
}
|
0d645857-f3d3-40a2-a461-1f942a58157b
|
public void setAxisLength(double length) {
this.axisLength = length;
}
|
0e554df0-d62e-4964-935f-7ea07a99df4d
|
public void setDashLength(double length) {
this.dashLength = length;
}
|
22810d16-f595-4383-baa6-03294af84ecf
|
public void setUnitLength(double length) {
this.unitLength = length;
}
|
d3ba29e2-7f7b-475f-9adf-fe5a5e056c08
|
public void setUnitDistance(double dist) {
this.unitDistance = dist;
}
|
08621365-bada-4556-9e8f-b46f23d0a3ce
|
public void setInterval(double start, double end) {
this.interval = new Interval(start, end);
}
|
fe2c2a01-0edd-4d05-8a39-ee05b8d5b6cb
|
private void generateAxis() {
if (this.orientation == Orientation.HORIZONTAL)
axisLine = new Line2D.Double(startPoint, new Point2D.Double(
startPoint.x + this.axisLength, startPoint.y));
else
axisLine = new Line2D.Double(startPoint, new Point2D.Double(
startPoint.x, startPoint.y + this.axisLength));
}
|
7a3e56ec-0e50-432f-b5c8-0cd10f2e3b06
|
private void generateTicks() {
int index = 0;
String numStr;
for (double i = interval.start; index < ticks.length; i += unitDistance, index++) {
if (i >= 1000 || i <= -1000)
numStr = new DecimalFormat("0.##E0").format(i);
else
numStr = new DecimalFormat("0.00").format(i);
if (this.orientation == Orientation.HORIZONTAL)
ticks[index] = new Tick(new Point2D.Double(startPoint.x
+ index * unitLength, startPoint.y), numStr, -1);
else
ticks[index] = new Tick(new Point2D.Double(startPoint.x,
axisLength + startPoint.y - index * unitLength),
numStr, 1);
}
}
|
9ffdecae-fee5-48a2-9090-2cb8671caf2c
|
private void drawTickLine() {
g2.setColor(TICK_LINE_COLOR);
for (int i = 0; i < ticks.length; i++) {
g2.draw(ticks[i].getTickLine());
if (this.orientation == Orientation.HORIZONTAL)
g2.drawString(ticks[i].getLabel(),
(int) ticks[i].getStartingPoint().x,
(int) ticks[i].getStartingPoint().y + 35);
else
g2.drawString(ticks[i].getLabel(),
(int) ticks[i].getStartingPoint().x - 45,
(int) ticks[i].getStartingPoint().y);
}
}
|
97b9e081-6a45-41b4-a1f6-e2ce442bc917
|
private void drawDashLine() {
g2.setStroke(DASH_STROKE);
g2.setColor(DASH_LINE_COLOR);
Line2D.Double dashLine;
for (int i = 1; i < ticks.length; i++) {
if (this.orientation == Orientation.HORIZONTAL)
dashLine = new Line2D.Double(ticks[i].getStartingPoint().x,
ticks[i].getStartingPoint().y - this.dashLength,
ticks[i].getStartingPoint().x,
ticks[i].getStartingPoint().y);
else
dashLine = new Line2D.Double(ticks[i].getStartingPoint().x,
ticks[i].getStartingPoint().y,
ticks[i].getStartingPoint().x + this.dashLength,
ticks[i].getStartingPoint().y);
g2.draw(dashLine);
}
}
|
403b150c-2ef4-41bd-b20b-2c3b3b4f659b
|
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D) g;
g2.draw(axisLine);
generateTicks();
drawTickLine();
drawDashLine();
}
|
680cbbff-f10d-46d1-b9d4-d7ca8ffe37f8
|
public ExpressionParser() {
this.postfixTokens = new ArrayList<String>();
this.token = new ArrayList<String>();
}
|
d9740af4-2447-4e98-beee-a776dc6a2014
|
public ExpressionParser(String exp) {
this();
this.infixExpression = exp;
tokenize();
infixToPostfix();
}
|
f74d598d-ff1f-41cd-8130-62c1f1d39fd7
|
public String getExpression() {
return this.infixExpression;
}
|
470b9b3c-bdc1-41b8-a806-8eaa18ce953e
|
public void setExpression(String exp) {
this.infixExpression = exp;
if (!this.postfixTokens.isEmpty())
this.postfixTokens.clear();
if (!this.token.isEmpty())
this.token.clear();
tokenize();
infixToPostfix();
}
|
5c39f630-2393-4d47-9027-8b72eea95c6f
|
public void setIsRadian(boolean b) {
this.isRadian = b;
}
|
dffe1ca7-eada-4b00-9ead-392e231d30b7
|
private int precedence(String operator) {
switch (operator) {
case "+":
case "-":
return 1;
case "*":
case "/":
return 2;
default:
return 3;
}
}
|
d30ba0f7-4d5b-40b7-9930-25b688c620f6
|
private int compareOps(String op1, String op2) {
return precedence(op1) - precedence(op2);
}
|
83727820-2331-45ca-8c40-cdd9955e0819
|
private boolean isNumeric(String str) {
try {
Double.parseDouble(str);
} catch (NumberFormatException n) {
return false;
}
return true;
}
|
12ac397d-f4ff-4101-a85b-4a72e891f244
|
private boolean isSpecialOps(String str) {
if (str.equals("sin") || str.equals("cos") || str.equals("tan")
|| str.equals("cot") || str.equals("ln") || str.equals("log"))
return true;
return false;
}
|
567a3086-fa59-40b8-b235-5089a67b46f1
|
private double degreeToRadian(double deg) {
return deg * Math.PI / 180;
}
|
9e356200-15ee-48be-b59a-8a5d5ef9062d
|
private void tokenize() {
StreamTokenizer st = new StreamTokenizer(new StringReader(
this.infixExpression));
// Make '/' readable. Otherwise, it stops at '/'
st.ordinaryChar('/');
try {
while (st.nextToken() != StreamTokenizer.TT_EOF) {
switch (st.ttype) {
case StreamTokenizer.TT_NUMBER:
token.add(String.valueOf(st.nval));
break;
case StreamTokenizer.TT_WORD:
token.add(st.sval);
break;
default:
token.add(String.valueOf((char) st.ttype));
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
|
64258718-25c9-4c6b-94d9-00d746106053
|
private void infixToPostfix() {
Stack<String> opStack = new Stack<String>();
for (int i = 0; i < token.size(); i++) {
if (isNumeric(token.get(i)) || token.get(i).equals("x"))
postfixTokens.add(token.get(i));
else if (token.get(i).equals("("))
opStack.push(token.get(i));
else if (token.get(i).equals(")")) {
while (!opStack.peek().equals("("))
postfixTokens.add(opStack.pop());
opStack.pop();
if(!opStack.isEmpty() && isSpecialOps(opStack.peek()))
postfixTokens.add(opStack.pop());
} else if (opStack.isEmpty() || opStack.peek().equals("(")
|| compareOps(opStack.peek(), token.get(i)) <= 0)
opStack.push(token.get(i));
else {
while (!opStack.isEmpty() && !opStack.peek().equals("(")
&& compareOps(opStack.peek(), token.get(i)) >= 0)
postfixTokens.add(opStack.pop());
opStack.push(token.get(i));
}
}
while (!opStack.isEmpty())
postfixTokens.add(opStack.pop());
}
|
65a5e6a6-0237-4e1e-9421-56ab350fe93b
|
private double perform(String operator, double num) {
double trigNum = num;
if (!this.isRadian)
trigNum = degreeToRadian(num);
switch (operator) {
case "sin":
return Math.sin(trigNum);
case "cos":
return Math.cos(trigNum);
case "tan":
if (Math.cos(trigNum) == 0)
throw new IllegalArgumentException("Angel is divisible by 90.");
return Math.sin(trigNum) / Math.cos(trigNum);
case "cot":
if (Math.sin(trigNum) == 0)
throw new IllegalArgumentException("Angel is divible by 180.");
return Math.cos(trigNum) / Math.sin(trigNum);
case "ln":
return Math.log(num);
case "log":
return Math.log10(num);
default:
throw new IllegalArgumentException("Illegal operator.");
}
}
|
cb261d51-18b9-440e-a772-bd8f1ad70006
|
private double perform(String operator, double num1, double num2) {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
if (num2 == 0)
throw new IllegalArgumentException("Divisor is 0.");
return num1 / num2;
case "^":
return Math.pow(num1, num2);
default:
throw new IllegalArgumentException("Illegal Operator.");
}
}
|
c33cb43d-87e7-4558-be2c-5f66045d03fa
|
public double eval(double x) {
Stack<Double> numStack = new Stack<Double>();
for (int i = 0; i < postfixTokens.size(); i++) {
if (isNumeric(postfixTokens.get(i)))
numStack.push(Double.parseDouble(postfixTokens.get(i)));
else if (postfixTokens.get(i).equals("x"))
numStack.push(x);
else if (isSpecialOps(postfixTokens.get(i))) {
double num = numStack.pop();
numStack.push(perform(postfixTokens.get(i), num));
} else {
try {
double num2 = numStack.pop();
double num1 = numStack.pop();
numStack.push(perform(postfixTokens.get(i), num1, num2));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
return numStack.pop();
}
|
dab37988-f158-4b97-99a9-8d9c94f35501
|
public ArrayList<Double> eval(Interval interval, double steps) {
ArrayList<Double> numList = new ArrayList<Double>();
for (double i = interval.start; i <= interval.end; i += steps) {
numList.add(eval(i));
}
return numList;
}
|
b048810b-5671-484d-b491-7d42243b1a3d
|
public PlotFunctionFrame(){
super("Plot Function");
pnlUpper.setLayout(new GridLayout(0, 3));
pnlUpper.add(new JLabel("f(x)"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtFunction);
pnlUpper.add(new JLabel("Start"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtStart);
pnlUpper.add(new JLabel("End"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtEnd);
pnlUpper.add(new JLabel("X Ticks"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtXTicks);
pnlUpper.add(new JLabel("Y Ticks"));
pnlUpper.add(new JLabel("="));
pnlUpper.add(txtYTicks);
cmbTrig.addItem("Degree");
cmbTrig.addItem("Radian");
cmbTrig.addActionListener(this);
pnlLower.add(cmbTrig);
btnPlot.addActionListener(this);
pnlLower.add(btnPlot);
add(pnlUpper, BorderLayout.NORTH);
add(pnlLower, BorderLayout.SOUTH);
setSize(280, 200);
}
|
ffd42c73-0070-4883-bb56-adce723a7aa7
|
public void clearEntry(){
this.txtStart.setText(null);
this.txtEnd.setText(null);
this.txtXTicks.setText(null);
this.txtYTicks.setText(null);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.