728x90
problem 06
피보나치 클래스를 만들되 for과 while을 사용하지 말라는 재귀함수 문제이다.
요렇게 생긴 코드를 기본으로 활용하되, 여기에 추가적인 class를 정의하라는 것으로 보인다.
문제는 dp를 사용할 수 없고 InSequence는 interface로 보이기 때문에 구현을 두가지를 해주어야 한다. 나머지는 점화식에 맞추어서 변수를 저장하고 출력하는 과정을 반복하면 쉽게 문제를 풀 수 있다.
interface IntSequence{
boolean hasNext();
int next();
}
class FibonacciSequence implements IntSequence{
private int n;
private int pn;
private int ppn;
public FibonacciSequence() {
n=-1;
pn=1;
ppn=0;
}
public boolean hasNext() {
return (n++)!=20;
}
public int next() {
if(n==0) return 0;
if(n==1) return 1;
int ans = pn+ppn;
ppn=pn;
pn=ans;
return ans;
}
}
public class Problem06 {
public static void main(String[] args) {
IntSequence seq = new FibonacciSequence();
for (int i = 0; i < 20; i++) {
if (seq.hasNext() == false)
break;
System.out.print(seq.next() + " ");
}
System.out.println(" ");
}
}
Problem 07
이번에도 메인은 정해져 있고 이것에 맞추어 출력을 하라는 것으로 보인다.
import java.util.Scanner;
interface IntSequenceStr{
abstract public boolean hasNext();
abstract public int next();
}
class BinarySequenceStr implements IntSequenceStr{
private int n;
private double p;
BinarySequenceStr(int n){
this.n=n;
p=Math.pow(2, 23);
while(p!=0) {
if((this.n & (int)p)>0) break;
p/=2;
}
}
public boolean hasNext() {
return p>=1;
}
public int next() {
int temp = this.n & (int)p;
p/=2;
return temp>0?1:0;
}
}
public class Problem07 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
String str = in.nextLine();
int num = Integer.parseInt(str);
in.close();
System.out.println("Integer: " + num);
IntSequenceStr seq = new BinarySequenceStr(num);
System.out.print("Binary number: ");
while(seq.hasNext()) System.out.print(seq.next());
System.out.println(" ");
}
}
Problem 08
그냥 Shape 객체 배열에 Circle, Rectangle, Square 객체를 포함할 수 있게 한다음 자바의 Dynamic Method Lookup 기능을 이용하여서 각 객체의 함수를 호출하면 되는 문제이다.
interface Shape {
public abstract double sum();
}
class Circle implements Shape {
private double r;
Circle(double r) {
this.r = r;
}
public double sum() {
return Math.pow(r, 2)* Math.PI;
}
}
class Square implements Shape {
private double l;
Square(double l) {
this.l = l;
}
public double sum() {
return l*l;
}
}
class Rectangle implements Shape {
private double w, h;
Rectangle(double w, double h) {
this.w = w;
this.h = h;
}
public double sum() {
return w * h;
}
}
public class Problem08 {
public static double sumArea(Shape[] arr) {
double ans = 0;
for (int i = 0; i < arr.length; ++i) {
ans += arr[i].sum();
}
return ans;
}
public static void main(String[] args) {
Shape[] arr = { new Circle(5.0), new Square(4.0), new Rectangle(3.0, 4.0), new Square(5.0) };
System.out.println("Total area of the shapes is: " + sumArea(arr));
}
}
Problem 09
Point라는 객체를 매개변수로 받는 EuclideanDistance와 ManhattanDistance클래스를 만드는 문제이다. 특별한 것은 없다... 예외처리만 잘해주자
단지 protected를 사용할 수 없기 때문에 따로 method를 만들어서 멤버변수에 접근 할 수 있도록 해주자
class Point{
private double[] d;
Point(double[] arr){
d= new double[arr.length];
d=arr;
}
public double getSize() {
return this.d.length;
}
public double getData(int i) {
return d[i];
}
}
class EuclideanDistance{
public static double getDist(Point p1,Point p2) {
if(p1==null) return -1;
if(p2==null) return -1;
if(p1.getSize()!=p2.getSize()) return -1;
double ans=0;
for(int i = 0 ; i<p1.getSize();++i) {
ans+=Math.pow(p1.getData(i)-p2.getData(i),2);
}
return Math.sqrt(ans);
}
}
class ManhattanDistance{
public static double getDist(Point p1,Point p2) {
if(p1==null) return -1;
if(p2==null) return -1;
if(p1.getSize()!=p2.getSize()) return -1;
double ans=0;
for(int i = 0 ; i<p1.getSize();++i) {
ans+=Math.abs(p1.getData(i)-p2.getData(i));
}
return ans;
}
}
public class Problem09 {
public static void main(String[] args) {
Point p1 = new Point(new double[] { 1.0, 2.0, 3.0 });
Point p2 = new Point(new double[] { 4.0, 5.0, 6.0 });
System.out.println("Euclidean Distance: " + EuclideanDistance.getDist(p1, p2));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p1, p2));
Point p3 = new Point(new double[] { 1.0, 2.0, 3.0 });
Point p4 = new Point(new double[] { 4.0, 5.0 });
System.out.println("Euclidean Distance: " + EuclideanDistance.getDist(p3, p4));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p3, p4));
}
}
Problem 10
마지막 문제는 Object 클래스 함수 오버라이딩과 관련된 문제이다. 그냥 문제에 맞게 수정해 주면 된다.
class Points {
private double[] arr;
Points(double[] arr) {
this.arr = new double[arr.length];
this.arr = arr;
}
public boolean equals(double[] obj) {
if(obj==null) return false;
if(obj.length!=arr.length) return false;
for(int i = 0 ; i<arr.length;++i) {
if(arr[i]!=obj[i]) return false;
}
return true;
}
public String toString() {
double ans=0;
for(int i = 0;i<arr.length;++i) {
ans+=arr[i];
}
return "[sum of points: "+ans+"]";
}
}
public class Problem10 {
public static void main(String[] args) {
Points p1 = new Points(new double[] { 1.0, 2.0, 3.0 });
Points p2 = new Points(new double[] { 4.0, 5.0, 6.0 });
System.out.println(p1);
System.out.println(p2);
System.out.println(p1.equals(p2));
Points p3 = new Points(new double[] { 1.0, 4.0, 7.0 });
Points p4 = new Points(new double[] { 3.0, 9.0 });
System.out.println(p3);
System.out.println(p4);
System.out.println(p3.equals(p4));
Points p5 = new Points(new double[] { 1.0, 2.0 });
Points p6 = null;
System.out.println(p5);
System.out.println(p6);
System.out.println(p5.equals(p6));
}
}
728x90
'CS(Computer Science) > 20) 자바' 카테고리의 다른 글
자바를 자바 14(Generic Programming) (0) | 2020.11.16 |
---|---|
자바를 자바 13(Exception Handling) (0) | 2020.11.16 |
자바를 자바 11 (0) | 2020.11.16 |
자바를 자바 10 (0) | 2020.11.16 |
자바를 자바 09 (0) | 2020.11.16 |