List of usage examples for java.lang Math PI
double PI
To view the source code for java.lang Math PI.
Click Source Link
From source file:MainClass.java
public static void main(String args[]) { try {//from w w w . j a v a 2 s. c o m InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); while (true) { System.out.print("Radius? "); String str = br.readLine(); double radius; try { radius = Double.valueOf(str).doubleValue(); } catch (NumberFormatException nfe) { System.out.println("Incorrect format!"); continue; } if (radius <= 0) { System.out.println("Radius must be positive!"); continue; } double area = Math.PI * radius * radius; System.out.println("Area is " + area); return; } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { final int width = 512; final int height = 512; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); Graphics g = img.getGraphics(); g.setColor(Color.black);// w w w . ja v a 2 s.com g.fillRect(0, 0, width, height); g.setColor(Color.white); final double A = 8; final double B = 0.5; final double N = 4; final double scale = 128; final double zoom = 50; final double step = 1 / scale; Point last = null; final Point origin = new Point(width / 2, height / 2); for (double t = 0; t <= 2 * Math.PI; t += step) { final double r = zoom * polarFunction(t, A, B, N); final int x = (int) Math.round(r * Math.cos(t)); final int y = (int) Math.round(r * Math.sin(t)); Point next = new Point(x, y); if (last != null) { g.drawLine(origin.x + last.x, origin.y + last.y, origin.x + next.x, origin.y + next.y); } last = next; } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JLabel(new ImageIcon(img))); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { DefaultTableModel model = new DefaultTableModel() { public Class getColumnClass(int columnIndex) { Object o = getValueAt(0, columnIndex); if (o == null) { return Object.class; } else { return o.getClass(); }//from w w w .j a v a 2s . c om } }; JTable table = new JTable(model); model.addColumn("Boolean", new Object[] { Boolean.TRUE }); model.addColumn("Date", new Object[] { new Date() }); model.addColumn("Double", new Object[] { new Double(Math.PI) }); model.addColumn("Float", new Object[] { new Float(1.2) }); model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") }); model.addColumn("Number", new Object[] { new Integer(1) }); model.addColumn("Object", new Object[] { "object" }); JFrame f = new JFrame(); f.setSize(300, 300); f.add(new JScrollPane(table)); f.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { DefaultTableModel model = new DefaultTableModel() { public Class getColumnClass(int columnIndex) { Object o = getValueAt(0, columnIndex); if (o == null) { return Object.class; } else { return o.getClass(); }//from w w w. j ava 2s . com } }; JTable table = new JTable(model); model.addColumn("Boolean", new Object[] { Boolean.TRUE }); model.addColumn("Date", new Object[] { new Date() }); model.addColumn("Double", new Object[] { new Double(Math.PI) }); model.addColumn("Float", new Object[] { new Float(1.2) }); model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") }); model.addColumn("Number", new Object[] { new Integer(1) }); model.addColumn("Object", new Object[] { "object" }); Enumeration e = table.getColumnModel().getColumns(); TableColumn col = (TableColumn) e.nextElement(); col.setCellRenderer(table.getDefaultRenderer(Boolean.class)); col.setCellEditor(table.getDefaultEditor(Boolean.class)); JFrame f = new JFrame(); f.setSize(300, 300); f.add(new JScrollPane(table)); f.setVisible(true); }
From source file:ArrayEnumerationFactory.java
public static void main(String args[]) { Enumeration e = makeEnumeration(args); while (e.hasMoreElements()) { System.out.println(e.nextElement()); }//w w w. j a va2 s.com e = makeEnumeration(new int[] { 1, 3, 4, 5 }); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } try { e = makeEnumeration(new Double(Math.PI)); } catch (IllegalArgumentException ex) { System.err.println("Can't enumerate that: " + ex.getMessage()); } }
From source file:ArraysTester.java
public static void main(String[] args) { ArraysTester tester = new ArraysTester(50); int[] myArray = tester.get(); // Compare two arrays int[] myOtherArray = tester.get().clone(); if (Arrays.equals(myArray, myOtherArray)) { System.out.println("The two arrays are equal!"); } else {/*from w ww. j av a 2 s .c o m*/ System.out.println("The two arrays are not equal!"); } // Fill up some values Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue()); myArray[30] = 98; // Print array, as is System.out.println("Here's the unsorted array..."); System.out.println(Arrays.toString(myArray)); System.out.println(); // Sort the array Arrays.sort(myArray); // print array, sorted System.out.println("Here's the sorted array..."); System.out.println(Arrays.toString(myArray)); System.out.println(); // Get the index of a particular value int index = Arrays.binarySearch(myArray, 98); System.out.println("98 is located in the array at index " + index); String[][] ticTacToe = { { "X", "O", "O" }, { "O", "X", "X" }, { "X", "O", "X" } }; System.out.println(Arrays.deepToString(ticTacToe)); String[][] ticTacToe2 = { { "O", "O", "X" }, { "O", "X", "X" }, { "X", "O", "X" } }; String[][] ticTacToe3 = { { "X", "O", "O" }, { "O", "X", "X" }, { "X", "O", "X" } }; if (Arrays.deepEquals(ticTacToe, ticTacToe2)) { System.out.println("Boards 1 and 2 are equal."); } else { System.out.println("Boards 1 and 2 are not equal."); } if (Arrays.deepEquals(ticTacToe, ticTacToe3)) { System.out.println("Boards 1 and 3 are equal."); } else { System.out.println("Boards 1 and 3 are not equal."); } }
From source file:Format.java
public static void main(String[] args) { System.out.format("%f, %1$+020.10f %n", Math.PI); }
From source file:TrigonometricDemo.java
public static void main(String[] args) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi is %.4f%n", Math.PI); System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians)); System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians)); System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians)); System.out.format("The arcsine of %.4f is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); System.out.format("The arccosine of %.4f is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians)))); System.out.format("The arctangent of %.4f is %.4f degrees %n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians)))); }
From source file:PrintfStuff.java
public static void main(String[] args) throws IOException { char three[] = { 't', 'h', 'r', 'e', 'e' }; System.out.printf("%b %n %c %n %s %n %s %n %d %n" + "%d %n %g %n %g %n %s %n", !false, '3', new String(three), "Three", 3, Long.MAX_VALUE, Math.PI, Double.MAX_VALUE, new Object()); }
From source file:TestFormat.java
public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); System.out.format("%08d%n", n); System.out.format("%+8d%n", n); System.out.format("%,8d%n", n); System.out.format("%+,8d%n%n", n); double pi = Math.PI; System.out.format("%f%n", pi); System.out.format("%.3f%n", pi); System.out.format("%10.3f%n", pi); System.out.format("%-10.3f%n", pi); System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); Calendar c = Calendar.getInstance(); System.out.format("%tB %te, %tY%n", c, c, c); System.out.format("%tl:%tM %tp%n", c, c, c); System.out.format("%tD%n", c); }