Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- public class Main {
- public static void write_raf(String path) throws IOException {
- RandomAccessFile file = null;
- try {
- // "r", "rw", "rws", "rwd"
- file = new RandomAccessFile(path, "rw");
- String imie = "Adam";
- String nazwisko = "Kowalski";
- int wiek = 50;
- char plec = 'M';
- double pensja = 2000;
- file.writeUTF(imie);
- file.writeUTF(nazwisko);
- file.writeInt(wiek);
- file.writeChar(plec);
- file.writeDouble(pensja);
- imie = "Julia";
- nazwisko = "Nowak";
- wiek = 30;
- plec = 'K';
- pensja = 2000;
- file.writeUTF(imie);
- file.writeUTF(nazwisko);
- file.writeInt(wiek);
- file.writeChar(plec);
- file.writeDouble(pensja);
- } finally {
- if (file != null) {
- file.close();
- }
- }
- }
- public static void read_raf(String path) throws IOException {
- RandomAccessFile file = null;
- try {
- // "r", "rw", "rws", "rwd"
- file = new RandomAccessFile(path, "rw");
- while (file.getFilePointer() < file.length()) {
- String imie = file.readUTF();
- String nazwisko = file.readUTF();
- int wiek = file.readInt();
- char plec = file.readChar();
- double pensja = file.readDouble();
- System.out.printf("Imię: %s\nNazwisko: %s\nWiek: %d\nPensja: %.2f\n", imie, nazwisko, wiek, pensja);
- }
- } finally {
- if (file != null) {
- file.close();
- }
- }
- }
- public static void modify_raf(String path) throws IOException {
- RandomAccessFile file = null;
- try {
- // "r", "rw", "rws", "rwd"
- file = new RandomAccessFile(path, "rw");
- while (file.getFilePointer() < file.length()) {
- String imie = file.readUTF();
- String nazwisko = file.readUTF();
- int wiek = file.readInt();
- char plec = file.readChar();
- long poz = file.getFilePointer();
- double pensja = file.readDouble();
- if (plec == 'K') {
- pensja = pensja * 2.0;
- file.seek(poz);
- file.writeDouble(pensja);
- }
- }
- } finally {
- if (file != null) {
- file.close();
- }
- }
- }
- public static void complex_mult(double [] u, double [] v, double [] res) {
- double a = u[0], b = u[1], c = v[0], d = v[1];
- res[0] = a * c - b * d;
- res[1] = a * d + b * c;
- }
- public static void complex_add(double [] u, double [] v, double [] res) {
- double a = u[0], b = u[1], c = v[0], d = v[1];
- res[0] = a + c;
- res[1] = b + d;
- }
- public static double complex_abs(double [] u) {
- return Math.sqrt(u[0] * u[0] + u[1] * u[1]);
- }
- public static void write_julia(String path) throws IOException {
- RandomAccessFile plik = null;
- try {
- plik = new RandomAccessFile(path, "rw");
- int w = 800;
- int h = 800;
- double re_min = -2.0;
- double re_max = 2.0;
- double im_min = -2.0;
- double im_max = 2.0;
- double [] c = new double [] { 0.0, 0.67 };
- plik.writeBytes("P5\n# Julia image\n");
- plik.writeBytes(w + " " + h + " 255\n");
- for (int i = 0; i < h; i++) {
- double im = (im_max - im_min) / h * i + im_min;
- for (int j = 0; j < w; j++) {
- double re = (re_max - re_min) / w * j + re_min;
- double [] z = new double [] { re, im };
- int n = 255;
- while ( complex_abs(z) < 10 && n >= 5 ) {
- complex_mult(z, z, z);
- complex_add(z, c, z);
- n -= 5;
- }
- plik.writeByte(n);
- }
- }
- } finally {
- if (plik != null) {
- plik.close();
- }
- }
- }
- public static void write_circle(String path) throws IOException {
- RandomAccessFile plik = null;
- try {
- plik = new RandomAccessFile(path, "rw");
- plik.writeBytes("P5\n");
- int promien = 200;
- plik.writeBytes((promien*2) + " " + (promien*2) + " 255\n");
- int k = 0;
- for (int i = 0; i < 2 * promien; i++) {
- for (int j = 0; j < 2 * promien; j++) {
- int x = i - promien;
- int y = j - promien;
- double odl = Math.sqrt(x * x + y * y);
- //double v = odl < promien ? 1 : 0 ;
- double v = (promien - odl) / promien;
- byte kolor = (byte)Math.max(0, Math.min(255, v * 255));
- plik.writeByte(kolor);
- ++k;
- }
- }
- } finally {
- if (plik != null) {
- plik.close();
- }
- }
- }
- public static void main(String[] args) throws IOException {
- write_raf("dane.dat");
- modify_raf("dane.dat");
- System.out.println("Po modyfikacji: ");
- read_raf("dane.dat");
- write_circle("circle.pgm");
- write_julia("julia.pgm");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement