{"id":467,"date":"2004-02-18T23:00:00","date_gmt":"2004-02-18T14:00:00","guid":{"rendered":"\/?p=467"},"modified":"2004-02-18T23:00:00","modified_gmt":"2004-02-18T14:00:00","slug":"sample-program","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/467.html","title":{"rendered":"sample program"},"content":{"rendered":"<p>\u3061\u3087\u3063\u3068\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u66f8\u3044\u3066\u304a\u304f\u3002<\/p>\n<table style=\"margin: 1px; background-color:#000000; color: #FFFFFF; white-space: pre; padding: 1px; border-width: 1px;\">\n<tr>\n<td>public class Reverse {<br \/>\n  private String s;<br \/>\n  private java.util.Stack stack = new java.util.Stack();<br \/>\n  public Reverse(String s_) throws Exception {<br \/>\n    if (s_.length() &lt; 1 || 1000 &lt; s_.length()) {<br \/>\n      throw new Exception(&#8220;\u6587\u5b57\u5217\u9577\u304c\u4e0d\u6b63\u3067\u3059&#8221;);<br \/>\n    }<br \/>\n    s = s_;<br \/>\n  }<br \/>\n  public String execute() {<br \/>\n    char[] cs = s.toCharArray();<br \/>\n    for (int i=0 ; i&lt;cs.length ; i++) {<br \/>\n      stack.push(new Character(cs[i]));<br \/>\n    }<br \/>\n    StringBuffer sb = new StringBuffer();<br \/>\n    while (!stack.empty()) {<br \/>\n      Character c = (Character)stack.pop();<br \/>\n      sb.append(c.charValue());<br \/>\n    }<br \/>\n    return new String(sb);<br \/>\n  }<br \/>\n  public static void main(String[] args) {<br \/>\n    try {<br \/>\n      String s = &#8220;0123456789&#8221;;<br \/>\n      Reverse rev = new Reverse(s);<br \/>\n      String rs = rev.execute();<br \/>\n      System.out.println(&#8220;\u5165\u529b\u6587\u5b57\u5217:&#8221;+s);<br \/>\n      System.out.println(&#8220;\u53cd\u8ee2\u6587\u5b57\u5217:&#8221;+rs);<br \/>\n    } catch(Exception e) {<br \/>\n      System.out.println(&#8220;\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f&#8221;);<br \/>\n    }<br \/>\n  }<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\n\u3000\n<\/p>\n<p><!--more--><\/p>\n<table style=\"margin: 1px; background-color:#000000; color: #FFFFFF; white-space: pre; padding: 1px; border-width: 1px;\">\n<tr>\n<td>import java.awt.Color;<br \/>\nimport java.awt.Image;<br \/>\nimport java.awt.Graphics;<br \/>\nimport java.util.Iterator;<br \/>\nimport java.util.Vector;<br \/>\npublic class Cg extends javax.swing.JFrame {<br \/>\n  interface Shape {<br \/>\n    void draw(Graphics g);<br \/>\n  }<br \/>\n  class Circle implements Shape {<br \/>\n    int x; int y; int r; Color c;<br \/>\n    public Circle(int x_, int y_, int r_, Color c_) { x=x_; y=y_; r=r_; c=c_;}<br \/>\n    public void draw(Graphics g) {<br \/>\n      Color oc = g.getColor();<br \/>\n      g.setColor(c);<br \/>\n      g.fillOval(x-r, y-r, 2*r, 2*r);<br \/>\n      g.setColor(oc);<br \/>\n    }<br \/>\n  }<br \/>\n  class Square implements Shape {<br \/>\n    int x; int y; int width; Color c;<br \/>\n    public Square(int x_, int y_, int w_, Color c_) { x=x_; y=y_; width=w_; c=c_;}<br \/>\n    public void draw(Graphics g) {<br \/>\n      Color oc = g.getColor();<br \/>\n      g.setColor(c);<br \/>\n      g.fillRect(x, y, width, width);<br \/>\n      g.setColor(Color.black);<br \/>\n      g.drawRect(x, y, width, width);<br \/>\n      g.setColor(oc);<br \/>\n    }<br \/>\n  }<br \/>\n  private Vector shapes = new Vector();<br \/>\n  private Graphics offscreen;<br \/>\n  private Image image;<br \/>\n  public Cg() {<br \/>\n    shapes.add(new Square(0, 0, 320, Color.white));<br \/>\n    shapes.add(new Square(120, 120, 80, Color.lightGray));<br \/>\n    shapes.add(new Circle(80,80,80, Color.black));<br \/>\n    shapes.add(new Square(160, 0, 80, Color.lightGray));<br \/>\n    shapes.add(new Square(240, 0, 80, Color.lightGray));<br \/>\n    shapes.add(new Square(240, 80, 80, Color.lightGray));<br \/>\n    shapes.add(new Circle(240,240,80, Color.black));<br \/>\n    shapes.add(new Square(0, 160, 80, Color.lightGray));<br \/>\n    shapes.add(new Square(0, 240, 80, Color.lightGray));<br \/>\n    shapes.add(new Square(80, 240, 80, Color.lightGray));<br \/>\n  }<br \/>\n  public void paint(Graphics g) {<br \/>\n    update(g);<br \/>\n  }<br \/>\n  public void update(Graphics g) {<br \/>\n    if (offscreen == null) {<br \/>\n      image = createImage(321, 321);<br \/>\n      offscreen = image.getGraphics();<br \/>\n      return;<br \/>\n    }<br \/>\n    Iterator iterator = shapes.iterator();<br \/>\n    while (iterator.hasNext()) {<br \/>\n      Shape s = (Shape)iterator.next();<br \/>\n      s.draw(offscreen);<br \/>\n    }<br \/>\n    g.drawImage(image, 10, 25, 321, 321, this);<br \/>\n  }<br \/>\n  public static void main(String[] args) {<br \/>\n    Cg cg = new Cg();<br \/>\n    cg.setSize(340, 360);<br \/>\n    cg.setVisible(true);<br \/>\n    cg.repaint();<br \/>\n  }<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u3061\u3087\u3063\u3068\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u66f8\u3044\u3066\u304a\u304f\u3002 public class Reverse { private String s; private java.util.Stack stack = new java.uti &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/467.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-467","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":0,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}