Uppgift 1
a)
public void minMax() {
int small = month[0], large = month[0];
for (int i=0; i large)
large = month[i];
if (month[i] < small)
small = month[i];
}
System.out.println("Största värdet="+large+", minsta="+small);
}
b) 7 4 1 -2c)
if (x>=4 && x<=7)
System.out.println("OK");
Uppgift 2
public class Sphere {
private double r,x,y,z;
public Sphere() {
this.x=0.0; this.y=0.0; this.z=0.0;
this.r=1.0;
}
public Sphere(double inx, double iny, double inz, double inr) {
this.r=inr; this.x=inx; this.y=iny; this.z=inz;
}
public double volume() {
double v = 4.0/3.0*Math.PI*Math.pow(this.r,3);
return v;
}
public void setRadius(double inr) {
this.r=inr;
}
public double getRadius() {
return this.r;
}
public String toString() {
String s = "x="+x+", y="+y+", z="+z + ", r="+r;
return s;
}
}
public class TestSphere {
public static void main(String[] arg) {
Sphere s1 = new Sphere();
Sphere s2 = new Sphere(1.0,2.0,3.0,4.0);
s1.setRadius(14.0);
double v = s1.volume();
System.out.println("volume = "+v);
double r = s2.getRadius();
System.out.println("radius = "+r);
System.out.println(s2);
}
}
Uppgift 3
import java.util.*;
public class Galaxer {
private int[] ellips;
private int[] medurs;
private int[] moturs;
public Galaxer() {
ellips = new int[10000];
medurs = new int[10000];
moturs = new int[10000];
}
public void mataIn(int bildNummer, int galaxTyp) {
if (galaxTyp==1) //elliptisk
ellips[bildNummer-1]++;
else if (galaxTyp==2) //spiral medurs
medurs[bildNummer-1]++;
else if (galaxTyp==3) //spiral moturs
moturs[bildNummer-1]++;
else
System.out.println("Felaktigt val.");
}
public void elliptiskaGalaxer() {
for (int i=0; i 3) {
System.out.println("Felaktig galaxtyp.");
return -1;
}
for (int i=0; i=antalPers) antal++;
}
else if (galaxTyp==2) {//spiral medurs
if (medurs[i]>=antalPers) antal++;
}
else if (galaxTyp==3) {//spiral moturs
if (moturs[i]>=antalPers) antal++;
}
}
return antal;
}
}
Uppgift 4
public class SimuleraMatchning {
public static void main (String[] arg ) {
// Skapa en population som kan innehålla 50 individer
Population pop = new Population(50);
Individ ny;
String id, id1, id2;
int [] egenskaper = new int[10];
int antal=50;
for (int i=0; i=0) {
// Ta reda på individernas identitet
id1=pop.getIndivid(index1).getId();
id2=pop.getIndivid(index2).getId();
System.out.println(id1 + " och " + id2 + " passar bäst. " +
" Matchningsvärdet="+min);
} // if
} // main
} // SimuleraMatchning
public class Population {
private Individ[] grupp;
private int antal;
public Population () {
this.grupp = new Individ[10];
this.antal=0;
}
public Population (int maxAnt) {
this.grupp = new Individ[maxAnt];
this.antal=0;
}
public void läggIn(Individ ny) {
this.grupp[this.antal]=ny;
this.antal++;
}
public int matchningsVärde(int index1, int index2) {
int v = this.grupp[index1].matchningsVärde( this.grupp[index2] );
return v;
}
public Individ getIndivid(int index) {
return this.grupp[index];
}
}
public class Individ {
private String id;
private int[] egenskap = new int[10];
public Individ() {
id="NN";
}
public Individ(String ident, int[] värden) {
this.id=ident;
for (int i=0; i<10; i++) {
egenskap[i]=värden[i];
}
}
public int matchningsVärde(Individ annan) {
int sum=0;
for (int i=0; i<10; i++) {
sum = sum + (int) (Math.pow( this.egenskap[i]-annan.egenskap[i], 2));
}
return sum;
}
public String getId() {
return this.id;
}
}