Skip to content

Instantly share code, notes, and snippets.

@skrb skrb/FizzBuzz.java
Created Jul 24, 2012

Embed
What would you like to do?
JJUG Project Lambda Hands-on Materials
import java.util.ArrayList;
import java.util.List;
public class FizzBuzz {
private static final String FIZZBUZZ = "FizzBuzz";
private static final String FIZZ = "Fizz";
private static final String BUZZ = "Buzz";
public FizzBuzz() {
List<Integer> numbers = initNumbers();
for (Integer num: numbers) {
// FizzBuzz の判別処理
if (num%15 == 0) {
System.out.println(FIZZBUZZ);
} else if (num%3 == 0) {
System.out.println(FIZZ);
} else if (num%5 == 0) {
System.out.println(BUZZ);
} else {
System.out.println(num);
}
}
}
private List<Integer> initNumbers() {
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i <= 100; i++) {
numbers.add(i);
}
return numbers;
}
public static void main(String... args) {
new FizzBuzz();
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.functions.Mapper;
public class NumberAdder {
Mapper<List<Integer>, Integer> mapper;
public NumberAdder() {
List<Integer> numbers = initNumbers();
// ここに mapper を使用して numbers の要素の総和を求める処理を書く
Integer sum = mapper.map(numbers);
System.out.println(sum);
}
private List<Integer> initNumbers() {
List<Integer> numbers = new LinkedList<>();
for (int i = 1; i <= 10; i++) {
numbers.add(i);
}
return numbers;
}
public static void main(String... args) {
new NumberAdder();
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ScoreFinder {
private Random random = new Random();
public ScoreFinder() {
List<Student> students = initStudents();
// ここに最大の成績を求める処理を記入
}
private List<Student> initStudents() {
List<Student> students = new ArrayList<>();
for (int i = 0; i <= 5_000_000; i++) {
students.add(new Student(""+i, random.nextInt(12)+2000, random.nextInt(101)));
}
return students;
}
public static void main(String... args) {
new ScoreFinder();
}
}
public class Student {
private String name;
private int gradYear;
private int score;
public Student(String name, int gradYear, int score) {
this.name = name;
this.gradYear = gradYear;
this.score = score;
}
public String getName() {
return name;
}
public int getGradYear() {
return gradYear;
}
public int getScore() {
return score;
}
public String toString() {
return "["+name+", year:" + gradYear + ", score:" + score + "]";
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingDemo {
private JTextField field;
private JLabel label;
public SwingDemo() {
JFrame frame = new JFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
field = new JTextField(20);
panel.add(field);
JButton button = new JButton("Update");
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
frame.add(label, BorderLayout.CENTER);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
label.setText(field.getText());
}
};
field.addActionListener(listener);
button.addActionListener(listener);
frame.setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.