midterm1: practice stuff

This commit is contained in:
Yuri Tatishchev 2025-03-11 23:18:20 -07:00
parent a80fe72611
commit 593840bd51
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
14 changed files with 268 additions and 0 deletions

29
midterm1/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
midterm1/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

6
midterm1/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
midterm1/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/midterm1.iml" filepath="$PROJECT_DIR$/midterm1.iml" />
</modules>
</component>
</project>

6
midterm1/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
midterm1/midterm1.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,30 @@
/**
* Animation Program that draws and moves a simple shape (e.g. butterfly, kite, etc.) on a timer event.
*/
public class Animation {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
javax.swing.JFrame frame = new javax.swing.JFrame("Animation");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
javax.swing.JPanel panel = new javax.swing.JPanel() {
@Override
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
g.setColor(java.awt.Color.RED);
g.fillOval(getX(), getY(), 30, 30);
}
};
frame.add(panel);
javax.swing.Timer timer = new javax.swing.Timer(100, (e) -> {
panel.setLocation((panel.getX() + 1) % 20, (panel.getY() + 1) % 20);
panel.repaint();
});
timer.start();
frame.setVisible(true);
});
}
}

58
midterm1/src/C4Q14.java Normal file
View File

@ -0,0 +1,58 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Write a program that shows a frame with three buttons labeled Red,
* Green, and Blue, and a label containing an icon showing a circle that is
* initially red. As the user clicks the buttons, the fill color of the circle should
* change. When you change the color, you need to invoke the repaint
* method on the label. The call to repaint ensures that the paintIcon method
* is called so that the icon can be repainted with the new color.
*/
public class C4Q14 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
javax.swing.JFrame frame = new javax.swing.JFrame("C4Q14");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
// frame.setLayout(new java.awt.BorderLayout());
javax.swing.JLabel label = new javax.swing.JLabel(new CircleIcon(java.awt.Color.RED, 300));
frame.add(label, java.awt.BorderLayout.CENTER);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setLayout(new java.awt.FlowLayout());
javax.swing.JButton redButton = new javax.swing.JButton("Red");
redButton.addActionListener((e) -> {
label.setIcon(new CircleIcon(java.awt.Color.RED, 300));
label.repaint();
});
panel.add(redButton);
javax.swing.JButton greenButton = new javax.swing.JButton("Green");
greenButton.addActionListener((e) -> {
label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300));
label.repaint();
});
greenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setIcon(new CircleIcon(java.awt.Color.GREEN, 300));
label.repaint();
}
});
panel.add(greenButton);
javax.swing.JButton blueButton = new javax.swing.JButton("Blue");
blueButton.addActionListener((e) -> {
label.setIcon(new CircleIcon(java.awt.Color.BLUE, 300));
label.repaint();
});
panel.add(blueButton);
// frame.add(panel, java.awt.BorderLayout.SOUTH);
frame.setVisible(true);
});
}
}

29
midterm1/src/C4Q6.java Normal file
View File

@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.Comparator;
public class C4Q6 {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<>(10);
a.add("a");
a.add("aa");
a.add("aaa");
a.add("aaaaa");
a.add("aaaa");
String max = maximum(a, (s1, s2) -> {
return Integer.compare(s1.length(), s2.length());
});
System.out.println(max);
}
public static String maximum(ArrayList<String> a, Comparator<String> c) {
String max = a.getFirst();
for (String s : a) {
if (c.compare(s, max) > 0) {
max = s;
}
}
return max;
}
}

21
midterm1/src/C4Q9.java Normal file
View File

@ -0,0 +1,21 @@
import java.util.Arrays;
import java.util.stream.Stream;
public class C4Q9 {
public static void main(String[] args) {
String[] a = {"a", "aa", "aaa", "aaaa", "aaaaa"};
String[] res = filter(a, (s) -> {
return s.length() <= 3;
});
for (String s : res) {
System.out.println(s);
}
}
public static String[] filter(String[] a, Filter f) {
return Arrays.stream(a).filter((s) -> {
return f.accept(s);
}).toArray(String[]::new);
}
}

View File

@ -0,0 +1,29 @@
import javax.swing.*;
import java.awt.*;
public class CircleIcon implements Icon {
private Color color;
private int size;
public CircleIcon(Color color, int size) {
this.color = color;
this.size = size;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillOval(x, y, size, size);
}
public int getIconWidth() {
return size;
}
public int getIconHeight() {
return size;
}
public void setColor(Color color) {
this.color = color;
}
}

3
midterm1/src/Filter.java Normal file
View File

@ -0,0 +1,3 @@
public interface Filter {
boolean accept(String x);
}

View File

@ -0,0 +1,24 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
/**
*
*/
public class MidtermQ1 {
public static void main(String[] args) {
}
}
class CirclePanel extends JPanel {
private Color color;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.draw(new Ellipse2D.Double(0, 0, 100, 100));
}
}