• 大小: 10KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: 其他
  • 标签: xhay  cqut  课程设计  

资源简介

7. 字符数统计(满分50分) 版本1:满分15分 Write a method that counts the number of letters in a string using the following header: public static int countLetters (String s) Write a test program that prompts the user to enter a string and displays the number of occurrences of letters in the string. 版本2:满分20分

资源截图

代码片段和文件信息


import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

/**
 * @Project name V7
 * @class name Test1
 * @author X-Hay
 * @Date 2013-9-9
 * @CQUT
 * @UP.
 */

public class ShowHistogram extends JPanel {
private String s;
private Histogram histogram = null;

public ShowHistogram(String s) {

this.s = s;
System.out.println(s);
int[] count = countLetters();
histogram = new Histogram(count);

setLayout(new BorderLayout());
add(histogram BorderLayout.CENTER);
repaint();
}

public String getS() {
return s;
}

public void setS(String newS) {
s = newS;
}

public int[] countLetters() {
// count for 26 letters
int[] count = new int[26];

// count occurrences of each letters
for (int i = 0; i < s.length(); i++) {
char character = s.charAt(i);

if (character >= ‘A‘ && character <= ‘Z‘) {
count[character - ‘A‘]++;
} else if (character >= ‘a‘ && character <= ‘z‘) {
count[character - ‘a‘]++;
}
}
return count;
}

class Histogram extends JPanel {
// count the occurrences of 26 letters
private int[] count;

public Histogram(int[] count){
this.count = count;
}

// paint the histogram
protected void paintComponent(Graphics g) {
// no display if count is null
if (count == null)
return;

super.paintComponent(g);
// find the panel size and bar width and interval dynamically
int width = getWidth();
int height = getHeight();
int interval = (width - 40) / count.length;
int invividualwidth = (int) (((width - 40) / 24) * 0.60);

// find the maximum count. the maximum count has the highest bar
int maxCount = 0;
for (int i = 0; i < count.length; i++) {
if (maxCount < count[i])
maxCount = count[i];

}
// x is the start position for the first bar in the histogram
int x = 30;

// draw a horizontal base line
g.drawLine(10 height - 45 width - 10 height - 45);
for (int i = 0; i < count.length; i++) {
// find the bar height
int barHeight = (int) (((double) count[i] / (double) maxCount) * (height - 55));

// display a bar (i.e.rectangle)
g.drawRect(x height - 45 - barHeight invividualwidth
barHeight);

// display a letter under the base line
g.drawString((char) (65 + i) + “ “ x height - 30);

// move x for display the next character
x += interval;
}
}

public Dimension getPreferredSize() {
return new Dimension(400 200);
}
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        301  2013-09-09 18:43  V7\.classpath

     文件        378  2013-09-09 18:43  V7\.project

     文件        598  2013-09-09 18:43  V7\.settings\org.eclipse.jdt.core.prefs

     文件       5272  2013-09-09 18:45  V7\.settings\org.eclipse.jdt.ui.prefs

     文件       1730  2013-09-09 21:56  V7\bin\ShowHistogram$Histogram.class

     文件       1460  2013-09-09 21:56  V7\bin\ShowHistogram.class

     文件       2210  2013-09-09 21:56  V7\bin\showJPanel$jbtShowListenner.class

     文件       1842  2013-09-09 21:56  V7\bin\showJPanel.class

     文件       1554  2013-09-09 21:56  V7\bin\Test1.class

     文件        389  2013-09-09 21:56  V7\bin\Test3.class

     文件       2548  2013-09-09 20:56  V7\src\ShowHistogram.java

     文件       2136  2013-09-09 21:03  V7\src\showJPanel.java

     文件        932  2013-09-09 19:31  V7\src\Test1.java

     文件        290  2013-09-09 20:07  V7\src\Test3.java

     目录          0  2013-09-09 18:45  V7\.settings

     目录          0  2013-09-09 21:56  V7\bin

     目录          0  2013-09-09 21:01  V7\src

     目录          0  2013-09-09 18:43  V7

----------- ---------  ---------- -----  ----

                21640                    18


评论

共有 条评论