Monday, June 26, 2017

Ch6: Swift Rectangle class example

import Foundation

class Rectangle {
    var length = 0
 var width = 0

 init(length: Int, width: Int) { // Constructor
  self.length = length
  self.width = width
 }
 var Area: Int{
  return length*width
 }

  func getArea() -> Int {
   return length*width
  }
}
class Circle {
    var radius = 0.0

 init(radius: Double) { // Constructor
  self.radius = radius

 }
 var Area: Double{
  return 3.14*(radius*radius)
 }

  func getArea() -> Double {
  return 3.14*(radius*radius)
  }
}

let rect1 = Rectangle(length: 10, width: 10)
print(rect1.Area)              // access the property
print(rect1.getArea())


let circ1=Circle(radius:12.0)
print(circ1.Area)
print(circ1.getArea())

Monday, June 19, 2017

CH5 Hellotextbox

package com.example.stu2000.hellotextbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends AppCompatActivity implements OnClickListener{
    private EditText editText;
    private TextView textView;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.editText);
        textView = (TextView)findViewById(R.id.textView2);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        textView.setText(editText.getText().toString());
    }

}