W6
COMPUTER PROGRAMMINING 2020 SPRING
W6 further in class concepts, introduction to composition & inheritance, class array structures
W6Ex1
public class book {
public String name,author; int year; public book(String namei,String
authori,int yeari) {name=namei;author=authori;year=yeari;} public book(book bi) {name=bi.name;author=bi.author;year=bi.year;} public String toString() {String s="book name =
"+name+" book author
= "+author+" publication
year = "+year; return s; } }
|
public class library {
public book b[]; String library_name; public library(String namei,book
bi[]) {library_name=namei; int n=bi.length; b=new book[n]; for(int
i=0;i<n;i++) {b[i]=bi[i];} } public String toString() {String s=" Library name : "+library_name+"\n"; int n=b.length; for(int
i=0;i<n;i++) {s+=b[i]+"\n";} return s; }} |
import javax.swing.JOptionPane; public class
W6Ex1 { public static void main(String arg[]) { book b1=new book("heat transfer","Özışık",1976); book b2=new book("the plaque","Camus",1987); book b3=new book("Computer programing","Çoban",2019); book
b[]={b1,b2,b3}; library lib=new library("week 6 library",b); JOptionPane.showMessageDialog(null,lib); } } |
import javax.swing.JOptionPane; public class
W6Ex1A { public static void main(String arg[]) {
book b1=new book("heat
transfer","Özışık",1976); book b2=new book("the
plaque","Camus",1987); book b3=new book("Computer
programing","Çoban",2019); book b[]=new
book[3]; for(int
i=0;i<b.length;i++) {b[i]=new book(b1);} librarya l=new librarya("week 6 librarya",b); JOptionPane.showMessageDialog(null,l); } } |
EX2
public class box {
double width,length,height; String bcolor; public box(double widthi,double
lengthi,double heighti,String
bc) {width=widthi;length=lengthi;height=heighti;bcolor=bc;} public double area() {return width*length+width*height+length*height;} public double volume() {return length*width*height;} public String toString() {String s="width = "+width+" m length = "+length+" m height =
"+height+" m area
= "+area()+" m"+'\u00B2'+" volume = "+volume()+"
m"+'\u00B3'+" color = "+bcolor+"\n"; return s; } } |
Composition:
red_box
public class red_box {
public box b; public red_box(double widthi,double lengthi,double heighti) {b=new box(widthi,lengthi,heighti,"red");} public String toString() {String
s=""+b; return s;} } |
Inheritance:
blue_box
public class blue_box extends box {
public blue_box(double widthi,double
lengthi,double heighti) {super(widthi,lengthi,heighti,"blue");} } |
Inheritance: yellow_box
public class yellow_box extends box {
public yellow_box(double widthi,double
lengthi,double heighti) {super(widthi,lengthi,heighti,"yellow");} } |
W6Ex2.java
import javax.swing.JOptionPane; public class
W6Ex2 { public static void main(String arg[]) {
box b1=new _box(1.2,2.3,1.5,"red"); box b2=new box(1.3,2.5,1.7,"blue"); box b3=new box(1.0,2.0,1.0,"yellow"); String
s=""+b1+b2+b3; JOptionPane.showMessageDialog(null,s); }} |
W6Ex2A.java
import javax.swing.JOptionPane; public class
W6Ex2A { public static void main(String arg[]) {
red_box b1=new red_box(1.2,2.3,1.5); blue_box b2=new blue_box(1.3,2.5,1.7,"blue"); box b3=new box(1.0,2.0,1.0,"yellow"); String
s=""+b1+b2+b3; JOptionPane.showMessageDialog(null,s); } } |
W6Ex2B.java
import javax.swing.JOptionPane; public class
W6Ex2B { public static void main(String arg[]) {
yellow_box b1=new yellow_box(1.2,2.3,1.5); blue_box b2=new blue_box(1.3,2.5,1.7); box b3=new box(1.0,2.0,1.0,"red"); box b4=new box(1.7,2.7,1.7,"green"); box
b[]={b1,b2,b3,b4}; String
s=""; for (int i=0;i<b.length;i++)
{s+=b[i];} JOptionPane.showMessageDialog(null,s); } } |
W6Ex3
complex class
public class complex {
public double real,imag; public complex(double r,double i) {real=r;imag=i;} public complex(complex c) {real=c.real;imag=c.imag;} public void add(complex
c) {real+=c.real; imag+=c.imag; } public static
complex add(complex c1,complex c2) {complex c3=new complex((c1.real+c2.real),(c1.imag+c2.imag)); return c3; } public String toString() {String
s="("+real+" + i*"+imag+")"; return s; } } |
W6Ex3.java
import javax.swing.JOptionPane; public class
W6Ex3 { public static void main(String arg[]) {
complex c1=new complex(1.2,2.3); complex c2=new complex(1.1,2.0); complex c3=complex.add(c1,c2); JOptionPane.showMessageDialog(null,""+c1+"+"+c2+"
= "+c3); }} |
W6Ex3A.java
import javax.swing.JOptionPane; public class
W6Ex3A { public static void main(String arg[]) {
complex c1=new complex(1.2,2.3); complex c2=new complex(1.1,2.0); complex c3=new complex(c1); c3.add(c2); JOptionPane.showMessageDialog(null,""+c1+"+"+c2+"
= "+c3); } } |
W6Ex3B.java
import javax.swing.JOptionPane; public class
W6Ex3B { public static void main(String arg[]) {
complex c1=new complex(1.2,2.3); complex c2=new complex(1.1,2.0); complex c3=new complex(c1); c3.multiply(c2); JOptionPane.showMessageDialog(null,""+c1+"+"+c2+"
= "+c3); }} |
W6Ex3C.java
import javax.swing.JOptionPane; public class
W6Ex3C { public static void main(String arg[]) {
complex c1=new complex(1.2,2.3); complex c2=new complex(1.1,2.0); complex c3=complex.multiply(c1,c2); JOptionPane.showMessageDialog(null,""+c1+"+"+c2+"
= "+c3); }} |
W6Ex4
public class vector {
public double x[]; //x=x[0]
y=x[1] z=x[2] public vector(double xi,double yi,double
zi) {double x1[]={xi,yi,zi}; x=x1; } public vector(double xi[]) {x=xi;} public vector(vector v) { x=new double[3]; for(int i=0;i<x.length;i++)
{x[i]+=v.x[i];} } public void add(vector
A) {for(int i=0;i<x.length;i++)
{x[i]+=A.x[i];}} public static
vector add(vector A,vector B) { vector C=new vector(A); C.add(B); return C; } //Scalar multiplication=dotproduct of two vectors // i*i=1 j*j=1 k*k=1 i*j=0 i*k=0 j*k=0 A.B public void dotproduct(vector A) {for(int i=0;i<x.length;i++)
{x[i]*=A.x[i];}} public static
vector dotproduct(vector A,vector B) { vector C=new vector(A); C.dotproduct(B); return C; } //Vectorel multiplication=crossproduct of two vectors // i*j=k j*k=i k*i=j j*i=-k k*j=-i i*k=-j //i*i=0 j*j=0 k*k=0 AxB public void crossproduct(vector B) { double x1=x[1]*B.x[2]-x[2]*B.x[1]; double y1=x[2]*B.x[0]-x[0]*B.x[2]; double z1=x[0]*B.x[1]-x[1]*B.x[0]; x[0]=x1;x[1]=y1;x[2]=z1; } public static vector crossproduct(vector A,vector B) { double x1=A.x[1]*B.x[2]-A.x[2]*B.x[1]; double y1=A.x[2]*B.x[0]-A.x[0]*B.x[2]; double z1=A.x[0]*B.x[1]-A.x[1]*B.x[0]; vector C=new vector(x1,y1,z1); return C; } public String toString() {String
s="["+x[0]+"*i+" + x[1]+"*j+"+
x[2]+"*k]"; return s; } } |
W6Ex4.java add
import javax.swing.JOptionPane; public class
W6Ex4 { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=vector.add(v1,v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }} |
W6Ex4A.java
add
import javax.swing.JOptionPane; public class
W6Ex4A { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=new vector(v1); v3.add(v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }}
|
W6Ex4B.java dotproduct
import javax.swing.JOptionPane; public class
W6Ex4A { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=new vector(v1); v3.dotproduct(v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }}
|
W6Ex4C.java dotproduct
import javax.swing.JOptionPane; import javax.swing.JOptionPane; public class
W6Ex4C { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=vector.dotproduct(v1,v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }} |
W6Ex4D.java crossproduct
import javax.swing.JOptionPane; public class
W6Ex4D { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=new vector(v1); v3.crossproduct(v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }} |
W6Ex4E.java crossproduct
import javax.swing.JOptionPane; public class
W6Ex4E { public static void main(String arg[]) {
vector v1=new vector(1.0,1.0,1.0); double
x[]={1,2,3}; vector v2=new vector(x); vector v3=vector.crossproduct(v1,v2); JOptionPane.showMessageDialog(null,""+v1+"
+ "+v2+" = "+v3); }} |
HOMEWORK
EXERCISES
W6HW1:
car and car registry classes are given
add the following
lis into the carregistry and list the
output
brand |
model |
year |
Ford |
fiesta |
2010 |
Ford |
mustang |
1968 |
Fiat |
Aegea |
2019 |
Fiat |
cinquocento |
1978 |
BMC |
austin |
1976 |
|
|
|
public class
car {
public String brand,model; int year; public car(String brandi,String
modeli,int yeari) {brand=brandi;model=modeli;year=yeari;} public String toString() {String
s="car brand = "+brand+"
model = "+model+" year = "+year+"\n"; return s; } }
|
public class
car_registry { public
car c[]; String registry_name; public car_registry(String namei,car ci[]) {registry_name=namei; int n=ci.length; c=new car[n]; for(int i=0;i<n;i++) {c[i]=ci[i];} } public String toString() {String s=" Registry : "+registry_name+"\n"; int n=c.length; for(int i=0;i<n;i++) {s+=c[i]+"\n";} return s; } } |
W6HW2
create Ford class a) by using composition
b) by using inheritance and add them into
the car_registry class
W6HW3:
calculate the following by using
complex class
Z1=3-2i Z2=1+3.2i Z3=( Z12+2)(
Z1 + Z2) Z4=(
Z1 - Z2)( Z1 + Z2)
W6HW4:
A force of F=10i+11j+2k is applied
to end of a bar with position of A=0.707106781i+0.707106781j+0k.
Find the Moment M=AxF (cross product
of this two vector)